feat: couple more lessons
This commit is contained in:
@@ -195,4 +195,45 @@ pub fn control_flow_module() {
|
|||||||
n @ 1..5 => println!("{n} is between 1 and 5"),
|
n @ 1..5 => println!("{n} is between 1 and 5"),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If let
|
||||||
|
// Lets you simplify destructuring
|
||||||
|
// Usefull if objecto neither implements nor derives PartialEq (cant do variable == Object::A)
|
||||||
|
if let n @ 1..5 = another_number {
|
||||||
|
println!("{n} is between 1 and 5");
|
||||||
|
}
|
||||||
|
|
||||||
|
// can also be used to match an enum
|
||||||
|
enum Fooo {
|
||||||
|
A,
|
||||||
|
B,
|
||||||
|
Another(u32),
|
||||||
|
};
|
||||||
|
|
||||||
|
let a = Fooo::A;
|
||||||
|
let b = Fooo::Another(3);
|
||||||
|
|
||||||
|
if let Fooo::A = a {
|
||||||
|
println!("a is A");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Fooo::Another(n) = b {
|
||||||
|
println!("b is another({n})");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let else
|
||||||
|
//<span class="underline"> Acts as a try - catch when delcaring variables
|
||||||
|
|
||||||
|
// while let
|
||||||
|
// Similar to if let, to make match sequences more tolerable
|
||||||
|
let mut optional = Some(0);
|
||||||
|
|
||||||
|
while let Some(i) = optional {
|
||||||
|
if i > 9 {
|
||||||
|
println!("Greater than 9");
|
||||||
|
optional = None;
|
||||||
|
} else {
|
||||||
|
optional = Some(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/functions.rs
Normal file
17
src/functions.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
fn fizzbuzz(n: i32) {
|
||||||
|
for i in 1..=n {
|
||||||
|
if i % 15 == 0 {
|
||||||
|
println!("FizzBuzz");
|
||||||
|
} else if i % 5 == 0 {
|
||||||
|
println!("Buzz");
|
||||||
|
} else if i % 3 == 0 {
|
||||||
|
println!("Fizz");
|
||||||
|
} else {
|
||||||
|
println!("{i}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn functions_module() {
|
||||||
|
fizzbuzz(10);
|
||||||
|
}
|
||||||
@@ -13,7 +13,8 @@
|
|||||||
//mod conversion;
|
//mod conversion;
|
||||||
// mod controlflow;
|
// mod controlflow;
|
||||||
// mod traits;
|
// mod traits;
|
||||||
mod str_types;
|
// mod str_types;
|
||||||
|
mod functions;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// helloworld::hello_world_module();
|
// helloworld::hello_world_module();
|
||||||
@@ -24,5 +25,6 @@ fn main() {
|
|||||||
//conversion::conversion_module();
|
//conversion::conversion_module();
|
||||||
// controlflow::control_flow_module();
|
// controlflow::control_flow_module();
|
||||||
//traits::traits_exercise();
|
//traits::traits_exercise();
|
||||||
str_types::str_types_module();
|
// str_types::str_types_module();
|
||||||
|
functions::functions_module();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user