My very first project in rust is a small poker like card game that has five columns players put five cards in. Card is a struct that fetches its value and suite from an external API. Board on the other hand, stores the columns I talked about earlier. Now, I want to test that a Board column doesn't get more than five cards by running a test.
Board:
struct Board {
columns: [Vec<Card>;5]
}
impl Board {
fn add_card(self){# adds a card}
Test:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_card_full_column() {
let card_one = Card::new(String::from("4"), String::from("Spades"));
let card_two = Card::new(String::from("3"), String::from("Spades"));
let card_three = Card::new(String::from("2"), String::from("Spades"));
let card_four = Card::new(String::from("5"), String::from("Spades"));
let card_five = Card::new(String::from("6"), String::from("Spades"));
let mut board = Board {
columns: [vec![], vec![], vec![], vec![], vec![]],
};
board.columns[0].push(card_one);
board.columns[0].push(card_two);
board.columns[0].push(card_three);
board.columns[0].push(card_four);
board.columns[0].push(card_five);
board.add_card();
assert_eq!(&board.columns[0].len(), 5);
}
}
rust-analyzer complains with can't compare &usize with integer (I'm aware of the usize borrow btw) so I've got a couple of questions:
- Do struct vectors allocate memory in a different way that uses
usizeinstead ofinteger? - I researched docs but didn't get anything I could use so I tried to cast
usizeto integer, I know that's not elegant but I wanted to pass the test and improve it later (green, red, refactor). I don't feel that casting the length is the answer. Should I still keep tying though?