added function and variable lessons.
This commit is contained in:
parent
4106843d70
commit
405437686a
8
the_book/branches/Cargo.toml
Normal file
8
the_book/branches/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
66
the_book/branches/src/main.rs
Normal file
66
the_book/branches/src/main.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
fn main() {
|
||||
let condition = true;
|
||||
let number = if condition { 5 } else { 6 };
|
||||
|
||||
if number % 4 == 0 {
|
||||
println!("number is divisible by 4");
|
||||
} else if number % 3 == 0 {
|
||||
println!("number is divisible by 3");
|
||||
} else if number % 2 == 0 {
|
||||
println!("number is divisible by 2");
|
||||
} else {
|
||||
println!("number is not divisible by 4, 3, or 2");
|
||||
}
|
||||
|
||||
//breaking out of multiple layers.
|
||||
let mut count = 0;
|
||||
'counting_up: loop {
|
||||
println!("count = {}", count);
|
||||
let mut remaining = 10;
|
||||
|
||||
loop {
|
||||
println!("remaining = {}", remaining);
|
||||
if remaining == 9 {
|
||||
break;
|
||||
}
|
||||
if count == 2 {
|
||||
break 'counting_up;
|
||||
}
|
||||
remaining -= 1;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
println!("End count = {}", count);
|
||||
|
||||
//returning loop
|
||||
let mut counter = 0;
|
||||
|
||||
let result = loop {
|
||||
counter += 1;
|
||||
|
||||
if counter == 10 {
|
||||
break counter * 2;
|
||||
}
|
||||
};
|
||||
|
||||
println!("The result is {}", result);
|
||||
|
||||
//while loop
|
||||
let mut number = 3;
|
||||
|
||||
while number != 0 {
|
||||
println!("{}!", number);
|
||||
|
||||
number -= 1;
|
||||
}
|
||||
|
||||
println!("LIFTOFF!!!");
|
||||
|
||||
//for loop
|
||||
let a = [10, 20, 30, 40, 50];
|
||||
|
||||
for element in a {
|
||||
println!("the value is: {}", element);
|
||||
}
|
||||
}
|
8
the_book/functions/Cargo.toml
Normal file
8
the_book/functions/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
16
the_book/functions/src/main.rs
Normal file
16
the_book/functions/src/main.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
fn main() {
|
||||
another_function(5);
|
||||
print_labeled_measurement(50, 'm');
|
||||
println!("The returned value is {}", plus_one(4));
|
||||
}
|
||||
|
||||
fn another_function(x: i32) {
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
||||
fn print_labeled_measurement(value: i32, unit_label: char) {
|
||||
println!("The measurement is: {}{}", value, unit_label);
|
||||
}
|
||||
//adds one and returns
|
||||
fn plus_one(x:i32) -> i32 {
|
||||
x+1
|
||||
}
|
8
the_book/variables/Cargo.toml
Normal file
8
the_book/variables/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
12
the_book/variables/src/main.rs
Normal file
12
the_book/variables/src/main.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
fn main() {
|
||||
let x = 5;
|
||||
|
||||
let x = x + 1;
|
||||
|
||||
{
|
||||
let x = x * 2;
|
||||
println!("The value of x in the inner scope is: {}", x);
|
||||
}
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
Loading…
Reference in a new issue