Learning Rust, Day 2

I was able to make minor progress on the Cribbage game. It’s been a few weeks since I had looked at it - turns out a global pandemic can be distracting. The code I left is just series of enums and a Card struct.

The goal for this round was simple: build an all function that returns all 52 cards and print the result. So, as a list:

This is roughly what I came up with for the “give me all the cards” function:

pub fn all() -> Vec<Card> {
    let mut cards = Vec::new();
    for suit in SUITS.iter() {
        for rank in RANKS.iter() {
            let card = Card::new(*rank, *suit);
            cards.push(card)
        }
    }
    return cards
}

So far so normal.

I did run into an interesting case: std::fmt::Display. I tried to implement it for the Card struct to make printing easier. This:

impl std::fmt::Display for Card {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

For some reason, this hilariously explodes with a stack overflow. It’s probably something silly. For now, I’m just doing println!("{}", card.to_string()); I’ll come back around to this next time so I can understand what’s going on.

Some minor notes:

That’s it for now.