How can i turn over a enum without extra effort

i have an enum which has two members

enum WhoMove {
    Left,
    Right,
}

i want just use a method like wm.turn_over() to turn over the wm, such as if the wm is WhoMove::Right, i hope that after used the method wm.turn_over, the wm will become WhoMove::Lift.
here is my code, but it can't work

enum WhoMove {
    Left,
    Right,
}

impl WhoMove {
    fn turn_over(&mut self) {
        // match self {
        //     WhoMove::Left => self = &mut WhoMove::Right,
        //     WhoMove::Right => self = &mut WhoMove::Left,
        // }
    }
}

Thank u for help!

You want something like this:

enum WhoMove {
    Left,
    Right,
}

impl WhoMove {
    fn turn_over(&mut self) {
        match self {
            WhoMove::Left => *self = WhoMove::Right,
            WhoMove::Right => *self = WhoMove::Left,
        }
    }
}
3 Likes

For my chess program I implemented a Not to flip the value (White, Black), which is quite convenient.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.