Is it possible to modify a vector element in place and use a value from the existing element that doesn't implement Copy
and do it without cloning?
enum Message {
A,
B(String),
C(String),
}
let mut v = vec![
Message::A,
Message::B("test".to_owned()),
Message::A,
Message::A,
];
if let m @ Message::B(msg) = &mut v[1] {
*m = Message::C(*msg);
}
This fails
error: borrow of moved value
--> src/main.rs:15:12
|
15 | if let m @ Message::B(msg) = &mut v[1] {
| -^^^^^^^^^^^^^^---^
| | |
| | value borrowed here after move
| value moved into `m` here
| move occurs because `m` has type `&mut Message` which does not implement the `Copy` trait
error[E0382]: borrow of moved value
--> src/main.rs:15:27
|
15 | if let m @ Message::B(msg) = &mut v[1] {
| ---------------^^^- --------- move occurs because value has type `&mut Message`, which does not implement the `Copy` trait
| | |
| | value borrowed here after move
| value moved here
error[E0507]: cannot move out of `*msg` which is behind a mutable reference
--> src/main.rs:16:25
|
16 | *m = Message::C(*msg);
| ^^^^ move occurs because `*msg` has type `String`, which does not implement the `Copy` trait