3

I have an enum that represents some state and a value that should be passed from one state to the next state.

struct MyValue {
    // data
}

enum MyState {
    Before(MyValue),
    After(MyValue),
}

Is there a way to write a method for MyState that transfers the ownership of MyValue from Before to After?

impl MyState {
    fn new() -> Self {
        let val = MyValue {};
        MyState::Before(val);
    }
    
    fn change_state(&mut self) {
        let val = todo!(); // move val out of Before into After without cloning the value
        *self = MyState::After(val);
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
x2bool
  • 2,766
  • 5
  • 26
  • 33

0 Answers0