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);
}
}