I'm trying to build a State Machine that holds a Token in some States. Is it possible to do this using only &mut self?
struct StateMachine{
variants:Variants,
}
enum State{
A(Token),
B(Token),
}
struct Token{}
impl StateMachine{
fn transition(&mut self){
match self.variants {
State::A(token) => {
self.variants = State::B(token);
},
_ => todo!(),
}
}
}
The Error:
error[E0507]: cannot move out of `self.variants.0` which is behind a mutable reference
--> testing/src/main.rs:20:15
|
20 | match self.variants {
| ^^^^^^^^^^^^^ help: consider borrowing here: `&self.variants`
21 | Variants::A(token) => {
| -----
| |
| data moved here
| move occurs because `token` has type `Token`, which does not implement the `Copy` trait
For more information about this error, try `rustc --explain E0507`.