Reputation: 35341
My code:
fn main() {
let mut messages = vec![];
let msg = Message::Write{message: "msg".to_string()};
match msg {
Message::Write{message} => println!("{}", message),
};
messages.push(msg);
}
enum Message {
Write{message: String},
}
The error:
error: use of partially moved value: `msg` [--explain E0382]
--> <anon>:9:19
6 |> Message::Write{message} => println!("{}", message),
|> ------- value moved here
...
9 |> messages.push(msg);
|> ^^^ value used here after move
note: move occurs because `msg.message` has type `std::string::String`, which does not implement the `Copy` trait
error: aborting due to previous error
It looks like the ownership of the message
field changes in the match
block. I just want to be able to output the value of the enum before adding it to the Vec
. How do I make this compile?
Upvotes: 6
Views: 7954
Reputation: 59005
Bind to the message
field by-reference instead of by-value.
match msg {
Message::Write{ref message} => println!("{}", message),
};
Upvotes: 17