Skip to content

Instantly share code, notes, and snippets.

@BartMassey
Created May 10, 2018 00:47
Show Gist options
  • Save BartMassey/c55ec9a4cc95a29678e087db8089abb5 to your computer and use it in GitHub Desktop.
Save BartMassey/c55ec9a4cc95a29678e087db8089abb5 to your computer and use it in GitHub Desktop.
Deck of cards in Rust
// Copyright © 2018 Bart Massey
// Simple deck of cards using enum_derive.
#[macro_use] extern crate custom_derive;
#[macro_use] extern crate enum_derive;
custom_derive! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, IterVariants(Suits))]
enum Suit {
Hearts,
Diamonds,
Clubs,
Spades,
}
}
custom_derive! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, IterVariants(Ranks))]
enum Rank {
Two = 2,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
Ace,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Card {
rank: Rank,
suit: Suit,
}
#[derive(Debug)]
struct Deck {
deck: Vec<Card>,
}
impl Deck {
fn new() -> Self {
let mut deck = Vec::new();
for rank in Rank::iter_variants() {
for suit in Suit::iter_variants() {
deck.push(Card{ rank, suit });
}
}
Deck { deck }
}
}
fn main() {
let deck = Deck::new();
for card in deck.deck {
println!("{:?}", card);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment