diff --git a/ch3/variables/src/main.rs b/ch3/variables/src/main.rs index b01d3ad..d2fdc98 100644 --- a/ch3/variables/src/main.rs +++ b/ch3/variables/src/main.rs @@ -138,6 +138,142 @@ fn main() { //------------------------------------------------------------------------ let num = plus_one(five()); println!("The value of num is: {num}"); + + //------------------------------------------------------------------------ + // ch3.4 About commments + + /* + This is a multiline comment pog + Documentation comments are covered in chapter 14. + */ + + //------------------------------------------------------------------------ + // ch3.5 control flow + + // let number = 3; + let number = 7; + + // The condition must evaluate to a bool. + if number < 5 { + println!("condition true"); + } else { + println!("condition false"); + } + + if number != 0 { + println!("Number is not 0"); + } + + let new_num = 6; + + if new_num % 4 == 0 { + println!("new_num is divisible by 4"); + } else if new_num % 3 == 0 { + println!("new_num divisible by 3"); + } else if new_num % 2 == 0 { + println!("new_num divisible by 2"); + } else { + println!("new_num not divisible by 4, 3, or 2"); + } + + //------------------------------------------------------------------------ + + let condition = true; + let cond_num = if condition { 5 } else { 6 }; // can't mismatch types this way. + println!("The value of cond_num is: {cond_num}"); + + //------------------------------------------------------------------------ + let val = if condition { 1 } else { 2 }; + let new_val; + if condition { + // referencing new_val from above within this if statement scope. + // Still a bit iffy on how the memory management works. + new_val = 1; + } else { + new_val = 2 + }; + println!("new_val = {new_val}"); + + //------------------------------------------------------------------------ + // Looping syntax + + let mut counter = 0; + // can grab a value from a loop statement + let result = loop { + println!("again!"); + if counter == 10 { + break counter * 2; + } + counter += 1; + }; + println!("The result is: {result}"); + + //------------------------------------------------------------------------ + // loop labels :eyes: + + let mut count = 0; + 'counting_up: loop { + // clojure where :eyes: + 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}"); + + //------------------------------------------------------------------------ + // While loop + + let mut while_num = 3; + + while while_num != 0 { + println!("while_num: {while_num}"); + while_num -= 1; + } + println!("LIFTOFFFFFFF (while_num reached 0)"); + + //------------------------------------------------------------------------ + // For loop + + let for_arr = [10, 20, 30, 40, 50]; + let mut index = 0; + + // The old, unpog way of doing this + while index < 5 { + println!("while value is: {}", for_arr[index]); + index += 1; + } + + // The new, pog way of doing this + for element in for_arr { + println!("for value is: {element}"); + } + + // for loop recreation of liftoff above + for pog_num in (1..4).rev() { + println!("{pog_num}"); + } + println!("for LIFTOFFFFFF!"); + + //------------------------------------------------------------------------ + // Summary questions at the end of the ch3.5 + + let ftc = fahrenheit_to_celsius(32.1); + println!("fahrenheit to celsius of 32.0 degress F: {ftc}"); + let ctf = celsius_to_fahrenheit(5.0); + println!("celsius_to_fahrenheitof 5.0 degrees C: {ctf}"); + let fib_num = fibonacci(5); + println!("fibonacci of fib_num: {fib_num}"); } // Statements: Instructions that perform some action and don't return anything @@ -166,3 +302,24 @@ fn five() -> i32 { fn plus_one(x: i32) -> i32 { x + 1 } + +// ch3.5 exercise +fn fahrenheit_to_celsius(temp: f64) -> f64 { + (temp - 32.0) * (5.0 / 9.0) +} + +// ch3.5 exercise +fn celsius_to_fahrenheit(temp: f64) -> f64 { + (9.0 / 5.0) * temp + 32.0 +} + +fn fibonacci(num: usize) -> usize { + if num == 0 { + return 0; + } else if num == 1 { + return 1; + } + fibonacci(num - 1) + fibonacci(num - 2) +} + +// I'm not programming the christmas carol song