Sub topic 12
Moves, references, and binding modes in patterns
This chapter assumes you know basic ownership and borrowing, how to define enums, and how match and destructuring work. It covers one question: when a pattern binds a variable, does the variable take ownership of the data, or does it borrow it?
Patterns can move data
A pattern that binds a name to a non-Copy value moves that value out of the matched place. Matching on msg by value moves the String inside Message::Text into s. After that, msg is partly or fully moved and you cannot use it again.
enum Message {
Text(String),
Quit,
}
fn main() {
let msg = Message::Text(String::from("hello"));
match &msg {
Message::Text(s) => println!("borrowed: {s}"),
Message::Quit => println!("quit"),
}
if let Message::Text(s) = msg {
println!("moved: {}", s.len());
}
}The first match uses &msg, so msg stays valid. The if let matches msg by value, so it moves the String into s. Using msg after that line would be a compile error.
Default binding modes
When you match a reference against a pattern that has no &, Rust switches to a reference binding mode. Every binding inside the pattern then becomes a reference too. In the first match, s has type &String, not String. You did not write & or ref in the pattern. The compiler inserted it for you.
The mode depends on what you match:
| You match | Binding `s` has type | Original value |
|---|---|---|
| `msg` (a value) | `String` | Moved out |
| `&msg` | `&String` | Still usable |
| `&mut msg` | `&mut String` | Still usable after the borrow ends |
Mutating through a pattern
Match on &mut to change data inside a variant without moving it.
enum Counter {
Count(u32),
Off,
}
fn bump(c: &mut Counter) {
match c {
Counter::Count(n) => *n += 1,
Counter::Off => {}
}
}
fn main() {
let mut c = Counter::Count(1);
bump(&mut c);
if let Counter::Count(n) = c {
println!("{n}");
}
}Here n is a &mut u32, so you write *n += 1 to change the number. The final if let can match c by value because u32 is Copy, so nothing is moved out.
Common errors
- Use after move. Matching a
String-carrying enum by value and then reading the enum later gives "borrow of partially moved value". Match on&valueinstead. - Forgetting to dereference. With a
&mutbinding,n += 1fails. Write*n += 1. - Moving out of a borrow. Matching
*referenceby value on a non-Copypayload fails with "cannot move out of a shared reference". Match the reference itself.
The `ref` and `ref mut` keywords
Older code writes Message::Text(ref s) to borrow a binding while matching by value. This still works. Prefer matching on &msg, which is shorter and covers every binding at once. Use ref only when you need to borrow one field and move another in the same pattern.
Exercise
A function takes msg: Message and runs match msg { Message::Text(s) => println!("{s}"), Message::Quit => {} }. After the call, the caller wants to keep using its own Message. What two changes let the caller keep it?
Show answer
Either change the function to take &Message and match on msg (or &msg), so s becomes &String, or have the caller pass a clone with msg.clone(). Borrowing is cheaper because it copies nothing. Cloning requires Message to derive Clone.