rust
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| rust [2024/07/11 20:32] – [Enums] utedass | rust [2024/07/11 20:46] (current) – [Matching] utedass | ||
|---|---|---|---|
| Line 227: | Line 227: | ||
| let absent_number: | let absent_number: | ||
| + | </ | ||
| + | |||
| + | ==== Matching ==== | ||
| + | <code rust> | ||
| + | enum UsState { | ||
| + | Alabama, | ||
| + | Alaska, | ||
| + | // --snip-- | ||
| + | } | ||
| + | |||
| + | enum Coin { | ||
| + | Penny, | ||
| + | Nickel, | ||
| + | Dime, | ||
| + | Quarter(UsState), | ||
| + | } | ||
| + | |||
| + | fn value_in_cents(coin: | ||
| + | match coin { | ||
| + | Coin::Penny => 1, | ||
| + | Coin:: | ||
| + | Coin::Dime => 10, | ||
| + | Coin:: | ||
| + | println!(" | ||
| + | 25 | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Can match integers as well | ||
| + | <code rust> | ||
| + | let dice_roll = 9; | ||
| + | match dice_roll { | ||
| + | 3 => add_fancy_hat(), | ||
| + | 7 => remove_fancy_hat(), | ||
| + | other => move_player(other), | ||
| + | } | ||
| + | |||
| + | fn add_fancy_hat() {} | ||
| + | fn remove_fancy_hat() {} | ||
| + | fn move_player(num_spaces: | ||
| </ | </ | ||
rust.1720729961.txt.gz · Last modified: 2024/07/11 20:32 by utedass
