In a match expression, the value being matched is moved into the pattern, transferring ownership to the matched arm.
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(u8, u8, u8),
}
fn main() {
let msg = Message::Write(String::from("hello"));
match msg {
Message::Write(text) => {
// `text` owns the String here; `msg` is consumed
println!("Text: {text}");
},
_ => {},
}
// msg cannot be used here; it was moved into the match
}
If you need to use the value after the match, match on a reference (&msg) instead to borrow it.