So I'd like to use enums to model state in a state machine. I'm having problems when it comes to ownership, though. Here's what I'm basically trying to do: https://gist.github.com/mmlinford/c99655a37391ae7bc7fc5da39e73a75b
But of course I get:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:28:28
|
28 | self.state = match self.state {
| ^^^^ cannot move out of borrowed content
29 | State::A(my_thing_a) => {
| ---------- hint: to prevent move, use `ref my_thing_a` or `ref mut my_thing_a`
...
33 | State::B(my_thing_b) => {
| ---------- ...and here (use `ref my_thing_b` or `ref mut my_thing_b`)
I'm able to sort of get what I want by putting the state field in an Option
, then take()
ing the content out, doing the operation that consumes it, then putting it back in the Option
: https://gist.github.com/mmlinford/ead5d94994bc980652aeff0dfa03626c
This seems inefficient though, since it's now in an Option
and that take()
call must take time, right? Is there a better way to achieve what I want?