mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
881 B
44 lines
881 B
#[derive(Debug)] // so we can inspect the state in a minute
|
|
enum UsState {
|
|
Alabama,
|
|
Alaska,
|
|
// --snip--
|
|
}
|
|
|
|
impl UsState {
|
|
fn existed_in(&self, year: u16) -> bool {
|
|
match self {
|
|
UsState::Alabama => year >= 1819,
|
|
UsState::Alaska => year >= 1959,
|
|
// -- snip --
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Coin {
|
|
Penny,
|
|
Nickel,
|
|
Dime,
|
|
Quarter(UsState),
|
|
}
|
|
|
|
// ANCHOR: describe
|
|
fn describe_state_quarter(coin: Coin) -> Option<String> {
|
|
let Coin::Quarter(state) = coin else {
|
|
return None;
|
|
};
|
|
|
|
if state.existed_in(1900) {
|
|
Some(format!("{state:?} is pretty old, for America!"))
|
|
} else {
|
|
Some(format!("{state:?} is relatively new."))
|
|
}
|
|
}
|
|
// ANCHOR_END: describe
|
|
|
|
fn main() {
|
|
if let Some(desc) = describe_state_quarter(Coin::Quarter(UsState::Alaska)) {
|
|
println!("{desc}");
|
|
}
|
|
}
|