I want to update an enumerated variant when moving the field of the old variant to the new one without cloning:
enum X { X1(String), X2(String), } fn increment_x(x: &mut X) { *x = match *x { X::X1(s) => X::X2(s), X::X2(s) => X::X1(s), } }
This does not work because we cannot move s from &mut X :
error[E0507]: cannot move out of borrowed content --> src/lib.rs:7:16 | 7 | *x = match *x { | ^^ | | | cannot move out of borrowed content | help: consider removing the '*': 'x' 8 | X::X1(s) => X::X2(s), | - data moved here 9 | X::X2(s) => X::X1(s), | - ...and here
Please do not offer such things as the implementation of enum X { X1, X2 } and the use of struct S { variant: X, str: String } , etc. This is a simplified example. Imagine that there are many other fields in the variants and you want to move one field from one variant to another.
sinan source share