rust-book/enums/src/main.rs
2019-01-30 14:56:09 -05:00

98 lines
1.7 KiB
Rust

#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
Arizona,
Arkansaw,
California,
Colorado,
Connecticut,
Delaware,
Florida,
Georgia,
Hawaii,
Idaho,
Illinois,
Indianan,
Iowa,
Kansas,
Kentucky,
Louisiana,
Maine,
Maryland,
Massachusetts,
Michigan,
Minnesota,
Mississippi,
Missouri,
Montana,
Nebraska,
Nevada,
NewHampshire,
NewJersey,
NewMexico,
NewYork,
NorthCarolina,
NorthDakota,
Ohio,
Oklahoma,
Oregon,
Pennsylvania,
RhodeIsland,
SouthCarolina,
SouthDakota,
Tennessee,
Texas,
Utah,
Vermont,
Virginia,
Washington,
WestVirginia,
Wisconsin,
Wyoming,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
},
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
},
}
}
fn count_non_quarters(count: &mut u32, coin: Coin) {
// If let is like matching on one condition of a match statement
if let Coin::Quarter(state) = coin {
println!("State quarter from {:?}!", state);
} else {
*count += 1;
}
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None, // Match is exhaustive, must define the 'None' case
Some(i) => Some(i + 1),
}
}
fn main() {
println!("Hello, world!");
println!("{}", value_in_cents(Coin::Quarter(UsState::Michigan)));
}