Skip to content

Instantly share code, notes, and snippets.

@Blazing-Mike
Created October 12, 2021 21:37
Show Gist options
  • Save Blazing-Mike/56efaaafeaf8e83765310f45baff93c2 to your computer and use it in GitHub Desktop.
Save Blazing-Mike/56efaaafeaf8e83765310f45baff93c2 to your computer and use it in GitHub Desktop.
fn main() {
let age = 18;
if x < 18 {
println!('you are too young');
} else if x == 18 {
println!('you are just the right age');
} else {
println!(' wow, you are old, enjoy !!! ');
}
//while
fn while_statement() {
let mut x = 0;
while x != 42 {
x += 1;
}
println!("x is {}", x);
}
// for
fn main() {
for x in 0..5 {
println!("{}", x);
}
for x in 0..=5 {
println!("{}", x);
}
}
// match / same as switch statement in JS
fn main() {
let x = 42;
match x {
0 => {
println!("found zero");
}
// we can match against multiple values
1 | 2 => {
println!("found 1 or 2!");
}
// we can match against ranges
3..=9 => {
println!("found a number 3 to 9 inclusively");
}
// we can bind the matched number to a variable
matched_num @ 10..=100 => {
println!("found {} number between 10 to 100!", matched_num);
}
// this is the default match that must exist if not all cases are handled
_ => {
println!("found something else!");
}
}
}
// from https://tourofrust.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment