I'm writing code to initialize a deck with 52 cards and shuffle them. In Java, I use an ArrayList
and iterate through the Suit
enum and the Rank
enum, adding a Card(suit,rank)
object as I go along. I then use Collections.shuffle()
.
I am trying to port this code to Rust, using vectors and structs. The problem is that I can't iterate enums like in Java. What is the Rust idiomatic way of trying to achieve this result?
I have tried importing strum & strum_macros to get enum iteration, but I am stuck with trying to push structs onto the Vec
and then randomly shuffle it.
Java Code
public class Deck {
private List < Card > aCards = new ArrayList < > ();
public Deck() {
shuffle();
}
public void shuffle() {
aCards.clear();
for (Suit suit: Suit.values()) {
for (Rank rank: Rank.values()) {
aCards.add(new Card(rank, suit));
}
}
Collections.shuffle(aCards);
}
}
Rust Attempt
use crate::card::Card;
use crate::rank::Rank;
use crate::suit::Suit;
use rand::{thread_rng, Rng};
use strum::IntoEnumIterator;
pub struct Deck {
cards: Vec<Card>,
}
impl Deck {
pub fn shuffle(&mut self) -> () {
self.cards.clear();
for s in Suit::iter() {
for r in Rank::iter() {
self.cards.push(Card::new(s, r));
}
}
}
}
struct for suit (rank is similar)
use strum_macros::*;
#[derive(EnumIter, Debug)]
pub enum Suit {
SPADES,
DIAMONDS,
CLUBS,
HEARTS,
}
card struct
pub struct Card {
suit: Suit,
rank: Rank,
}
impl Card {
pub fn new(psuit: Suit, prank: Rank) -> Card {
Card {
suit: psuit,
rank: prank,
}
}
}
I want to just simply iterate trough two sets of enum variants then shuffle output pairs but this is seemingly much more complicated! I suspect maybe there is a better way?