rust-ape-example/src/bin/flow_control_match_guard.rs
2022-09-07 11:36:05 +05:30

39 lines
926 B
Rust

// ./src/flow_control/match/guard.md
enum Temperature {
Celsius(i32),
Fahrenheit(i32),
}
fn part0() {
let temperature = Temperature::Celsius(35);
// ^ TODO try different values for `temperature`
match temperature {
Temperature::Celsius(t) if t > 30 => println!("{}C is above 30 Celsius", t),
// The `if condition` part ^ is a guard
Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t),
Temperature::Fahrenheit(t) if t > 86 => println!("{}F is above 86 Fahrenheit", t),
Temperature::Fahrenheit(t) => println!("{}F is below 86 Fahrenheit", t),
}
}
fn part1() {
let number: u8 = 4;
match number {
i if i == 0 => println!("Zero"),
i if i > 0 => println!("Greater than zero"),
_ => unreachable!("Should never happen."),
// TODO ^ uncomment to fix compilation
}
}
pub fn main() {
part0();
part1();
}