Hi there. I am trying to learn Rust and going through the rust-lang.org book but I am a bit unclear on enums.
Is there a way to use a None/Null or default value for coins that don't have a state? I had tried returning an empty string in the tuple, but it requires the UsState type. Then I just decided to have a UsState::None
, but I don't really know if that is good practice or not.
The example in the book just prints the state for a Coin::Quarter
but I want to return a state as well (if it's a quarter) as the first element in the tuple else some default value.
#[derive(Debug)]
enum UsState {
Alabama,
Massachusetts,
RhodeIsland,
None,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> (UsState, u8) {
match coin {
Coin::Penny => (UsState::None, 1),
Coin::Nickel => (UsState::None, 5),
Coin::Dime => (UsState::None, 10),
Coin::Quarter(state) => (state, 25),
}
}
fn main() {
let p: Coin = Coin::Penny;
println!("{:?}", value_in_cents(p));
let n: Coin = Coin::Nickel;
println!("{:?}", value_in_cents(n));
let d: Coin = Coin::Dime;
println!("{:?}", value_in_cents(d));
let mut q: Coin = Coin::Quarter(UsState::Massachusetts);
println!("{:?}", value_in_cents(q));
q = Coin::Quarter(UsState::Alabama);
println!("{:?}", value_in_cents(q));
q = Coin::Quarter(UsState::RhodeIsland);
println!("{:?}", value_in_cents(q));
}```