Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Last active March 11, 2018 19:42
Show Gist options
  • Save cadwallion/73d7eeaf73d7647a324e1d106a52b96c to your computer and use it in GitHub Desktop.
Save cadwallion/73d7eeaf73d7647a324e1d106a52b96c to your computer and use it in GitHub Desktop.
Card Display
enum Card {
Heart(i32),
Spade(i32),
Diamond(i32),
Club(i32),
}
impl Card {
fn value_to_string(&self, value: i32) -> String {
match value {
13 => String::from("K"),
12 => String::from("Q"),
11 => String::from("J"),
10 => String::from("T"),
1 => String::from("A"),
_ => value.to_string(),
}
}
fn display(&self) -> String {
match *self {
Card::Heart(val) => self.value_to_string(val) + "h",
Card::Spade(val) => self.value_to_string(val) + "s",
Card::Diamond(val) => self.value_to_string(val) + "d",
Card::Club(val) => self.value_to_string(val) + "c",
}
}
}
fn main() {
let first_card = Card::Heart(13);
println!(
"Your card is {}",
first_card.display()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment