#Assert struct vector length

28 messages · Page 1 of 1 (latest)

analog hazel
#

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:

  1. Do struct vectors allocate memory in a different way that uses usize instead of integer?
  2. I researched docs but didn't get anything I could use so I tried to cast usize to 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?
rose ingot
#

Do struct vectors allocate memory in a different way that uses usize instead of integer?
what exactly do you mean by this?

analog hazel
#

Yep it might sound weird (you're not saying that I know ;)) but I think it's a great opportunity to clarify concepts. Rust allocates memory in vectors but that allocation happens with another type of variable because this is not a vector of primitives but a vector of structures. I think I'm asking this because I had a bit of exposure with C an remember its size_t variable.

rose ingot
#

because this is not a vector of primitives but a vector of structures
makes no difference
C an remember its size_t variable.
yeah size_t is kind of like usize

#

also it should be noted that "integer" is not a type in Rust, that just refers to an int literal that the compiler hasn't decided that exact type of yet

analog hazel
#

Alright, that's something new for me

#

What am I missing in my assertion then if integer isn't a type. I've worked with other languages and this is confusing

analog hazel
#

assert_eq!(&board.columns[0].len(), 5);

rose ingot
#

you said you were aware of the usize borrow so I though you knew that was why it doesn't compile

#

?play ```rs
assert_eq!(&7, 4);

hasty brambleBOT
#
error[E0277]: can't compare `&{integer}` with `{integer}`
 --> src/main.rs:2:1
  |
2 | assert_eq!(&7, 4);
  | ^^^^^^^^^^^^^^^^^ no implementation for `&{integer} == {integer}`
  |
  = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}`
  = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider dereferencing here
 --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:46:22
  |
46|                 if !(**left_val == *right_val) {
  |                      +

For more information about this error, try `rustc --explain E0277`.```
rose ingot
#

?play ```rs
assert_eq!(1, 1);
assert_eq!(&7, &7);

hasty brambleBOT
analog hazel
#

Alrigh, I'm getting it. I'm basically trying to assert two different type of things, can't compare red apples with pears but red apples with red apples so to say

#

But I can't make the assertion because calling board.add_card() causes a moving which means I'm not able to use board again after that call.

rose ingot
#

then don't make add_card() move board

analog hazel
#

Sorry I don't know how to do that

#

Do you have an example please?

rose ingot
#

how much of the book have you read through?

analog hazel
#

I read it through and did all rustlings

#

Making a personal project feels completely different in my case

rose ingot
#

that should have been covered as part of structs/methods

#

anyway rs fn add_card(self){# adds a card} this takes ownership because the parameter is self rather than &self or &mut self

analog hazel
#

Of course

#

Thanks

#

Yeah that makes sense

#

There's a chapter in the book dedicated to borrowing, I'll come back to it.