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.