#[derive(Debug)] enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
}
pub fn match_def() {
let some_u8_value = Some(0);
let a = 15;
let i = value_in_cents(Coin::Penny);
println!("i is {}", i);
let i = value_in_cents(Coin::Quarter(UsState::Alabama));
println!("i is {}", i)
}
pub fn option_match() {
let five = Some(5);
let six = plus_one(five);
println!("six is {:?}", six);
let none = plus_one(None);
println!("none is {:?}", none)
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1)
}
}
pub fn match_other() {
let x = 8;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_other => println!("anything"),
}
match x {
1..=5 => println!("one through five"),
_ => println!("something else"),
}
match x {
1..=5 => println!("one through five"),
_ => (),
}
}