rust-book/enums/src/main.rs

96 lines
1.6 KiB
Rust
Raw Normal View History

2019-01-30 14:52:10 -05:00
#[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,
New_Hampshire,
New_Jersey,
New_Mexico,
New_York,
North_Carolina,
North_Dakota,
Ohio,
Oklahoma,
Oregon,
Pennsylvania,
Rhode_Island,
South_Carolina,
South_Dakota,
Tennessee,
Texas,
Utah,
Vermont,
Virginia,
Washington,
West_Virginia,
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),
}
}
2019-01-30 14:25:30 -05:00
fn main() {
println!("Hello, world!");
}