Compare commits
2 Commits
1529443824
...
cfc4436eeb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cfc4436eeb | ||
|
|
7b2a7eeb49 |
7
control_flow/Cargo.lock
generated
Normal file
7
control_flow/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "control_flow"
|
||||
version = "0.1.0"
|
||||
6
control_flow/Cargo.toml
Normal file
6
control_flow/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "control_flow"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
45
control_flow/src/main.rs
Normal file
45
control_flow/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
fn main() {
|
||||
//if else statements
|
||||
let mut num1 = 10;
|
||||
let mut num2 = 5;
|
||||
|
||||
if num1 < num2 {
|
||||
println!("{} is smaller then {}", num1, num2);
|
||||
} else {
|
||||
println!("{} is bigger then {}", num1, num2);
|
||||
}
|
||||
|
||||
//if expressions
|
||||
//always need a else case and must be of the same type, ternary operations do not exist but this results the same
|
||||
num1 = if num1 != num2 { 5 } else { 15 };
|
||||
println!("num 1: {}", num1);
|
||||
|
||||
//match
|
||||
//same as switch case
|
||||
match num1 {
|
||||
1 => println!("Number is 1"),
|
||||
2 => println!("Number is 2"),
|
||||
3 => println!("Number is 3"),
|
||||
6 => println!("Number is 6"),
|
||||
7 => println!("Number is 7"),
|
||||
_ => println!("Number is none of the other statements"),
|
||||
}
|
||||
// _ is a default and altough syntax specifies => it should be an exact match
|
||||
|
||||
//combining match results
|
||||
match num2 {
|
||||
1 | 2 | 3 | 4 => println!("Number is in range of 1 to 4"),
|
||||
5 | 7 => println!("Number is 5 or 7"),
|
||||
_ => println!("Number is none of the above"),
|
||||
}
|
||||
|
||||
//match expression
|
||||
println!("Old number for num2: {}",num2);
|
||||
let num2 = match num2 {
|
||||
1 | 2 | 3 | 4 => 1234,
|
||||
5 | 7 => 57,
|
||||
_ => -1,
|
||||
};
|
||||
println!("New number for num2: {}",num2);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user