Error: use of moved value:


#1

I am encountering a compiler error that states: Error: use of moved value: Here is the code

    fn testJRC(flag: Flag, shouldBeSet: bool, inst: u8) {
             //...
             //setFlag signature: fn setFlag(flag: Flag, F: &mut u8)

             setFlag(flag, &mut cpu.F);  //error: use of moved value: `flag`
            
             //...
    
    }
    testJRC(Flag::Zero, false, 0x20);

//The definition of Flag is:
enum Flag {
    Zero = 0x80,
    Neg = 0x40,
    Half = 0x20,
    Carry = 0x10
}

How does one make an enum type copyable? Would it be better for setFlag() have to take a &Flag instead of a Flag type?


#2

Types are copyable when they implement the Copy trait. The #[derive()] attribute can implement the trait for you:

#[derive(Copy, Clone)]
enum Flag {
...

You might also consider deriving PartialEq, Eq, Debug for comparison operators and printing.


#3

Thanks very much for this! Making the enum copyable got rid of the error message.