0

I am a beginner to rust and try to make a cardgame to practice. In stack.rs import_all_cards i try to generate all combinations of the enums the card can have. I know I could do it by maually writing the single cards and pushing them to the vector, but as mentioned I want to learn Rust so I want to know how this could work. The problem I guess my code has is, that the ownership of the enums is getting passed to the cards and are no longer owned by the vector. So the next iteration fails. Is this right and how do I manage to clone/copy the enums? (I am not quite sure but I think that enums don't have a clone or copy function.) This is the code:

File: card.rs

pub struct Card {
    colour: Colour,
    card_type: Card_type,
}
pub enum Colour {
    green,
    red,
    yellow,
}
pub enum Card_type {
    ace,
    ten,
    king,
    nine,
    eight,
    seven,

File: stack.rs

use crate::card::*;
 pub fn import_all_cards() -> Vec<Card>{
    let mut stack: Vec<Card> = Vec::new();
    let f = vec![Colour::green, Farbe::red, Farbe::yellow];
    let t = vec![Card_type::ace, Card_type::ten, Card_type::king, Card_type::nine, Card_type::eight, Card_type::seven];
        for farbe in f {
            for typ in t {
                stack.push(Card::new(colour, card_type));
            }
        }
    stack
}

File: main.rs

pub mod card;
pub mod stack;
use crate::card::*;
use crate::stack::*;

fn main() {
    let mut stack: Vec<Card> = import_all_cards()
}

So as you can see in the code, I tried to get all combinations of enums on cards and then pass them to a vector (stack of cards). The Error I got was:

error[E0382]: use of moved value.
  • 2
    Have you tried adding a `#[derive(Copy)]` to all your types? – rodrigo Feb 07 '23 at 16:04
  • Your examples contain invalid code (`type` is a reserved keyword) please always copy & paste the [mre] that you've confirmed to generate the error you're getting, also please provide the full error message (enclosed in ``` so it's properly formatted). – cafce25 Feb 07 '23 at 16:11
  • 1
    You'll want to get in the habit of tagging things with `#[derive(...)])` and include things like `Copy` or `Clone` where applicable. Bare `struct` definitions are fussy to work with. For types that are trivial to copy and are unlikely to get more complex in the future, add `Copy`. It'll save you a ton of hassle. – tadman Feb 07 '23 at 16:16
  • Thanks to all the replys. I corrected my mistake with the `type` keyword. This happened because I got this Error last time I coded and just wrote `type` in my own language. To put it on Stackoverflow I translated it quickly and totaly forgot about this Error. Sorry. – IliHanSoLow Feb 07 '23 at 20:15

0 Answers0