trpl-zh-cn/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs

32 lines
507 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
// --snip--
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
// ANCHOR: here
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
}
}
}
// ANCHOR_END: here
fn main() {
value_in_cents(Coin::Quarter(UsState::Alaska));
}