From 405437686ae2af004d06fa8bec27fef547b690f9 Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 20 Jun 2022 15:44:29 -0500 Subject: [PATCH] added function and variable lessons. --- the_book/branches/Cargo.toml | 8 +++++ the_book/branches/src/main.rs | 66 ++++++++++++++++++++++++++++++++++ the_book/functions/Cargo.toml | 8 +++++ the_book/functions/src/main.rs | 16 +++++++++ the_book/variables/Cargo.toml | 8 +++++ the_book/variables/src/main.rs | 12 +++++++ 6 files changed, 118 insertions(+) create mode 100644 the_book/branches/Cargo.toml create mode 100644 the_book/branches/src/main.rs create mode 100644 the_book/functions/Cargo.toml create mode 100644 the_book/functions/src/main.rs create mode 100644 the_book/variables/Cargo.toml create mode 100644 the_book/variables/src/main.rs diff --git a/the_book/branches/Cargo.toml b/the_book/branches/Cargo.toml new file mode 100644 index 0000000..6934aa4 --- /dev/null +++ b/the_book/branches/Cargo.toml @@ -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] diff --git a/the_book/branches/src/main.rs b/the_book/branches/src/main.rs new file mode 100644 index 0000000..76dade7 --- /dev/null +++ b/the_book/branches/src/main.rs @@ -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); + } +} diff --git a/the_book/functions/Cargo.toml b/the_book/functions/Cargo.toml new file mode 100644 index 0000000..a9b5578 --- /dev/null +++ b/the_book/functions/Cargo.toml @@ -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] diff --git a/the_book/functions/src/main.rs b/the_book/functions/src/main.rs new file mode 100644 index 0000000..a528f4d --- /dev/null +++ b/the_book/functions/src/main.rs @@ -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 +} diff --git a/the_book/variables/Cargo.toml b/the_book/variables/Cargo.toml new file mode 100644 index 0000000..f1cad76 --- /dev/null +++ b/the_book/variables/Cargo.toml @@ -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] diff --git a/the_book/variables/src/main.rs b/the_book/variables/src/main.rs new file mode 100644 index 0000000..606ee68 --- /dev/null +++ b/the_book/variables/src/main.rs @@ -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); +}