Sub topic 3
Reading and changing fields
This chapter assumes you can define a struct and create an instance, and that you know the basics of moves and borrows. It covers how you read fields, how you change them, and where the borrow checker draws its lines.
Reading fields with dot syntax
You read a field with value.field. The same syntax works on a struct value, a & reference, and a &mut reference. Rust dereferences automatically, so you never write (*r).field.
struct Player {
name: String,
score: u32,
}
fn bump(p: &mut Player) {
p.score += 1;
}
fn main() {
let mut p = Player {
name: String::from("Ada"),
score: 10,
};
println!("{} has {}", p.name, p.score);
bump(&mut p);
p.score += 5;
println!("{} now has {}", p.name, p.score);
}Mutability belongs to the binding
A struct has no per-field mut keyword. The whole instance is either mutable or not, depending on how the variable is declared. If you leave out mut, every field is read-only:
error[E0594]: cannot assign to `p.score`, as `p` is not declared as mutableThe fix is let mut p. When you pass the struct to a function, the parameter type decides. A &Player gives read-only access. A &mut Player allows assignment to any field.
Borrowing fields separately
Inside one function, the compiler tracks borrows per field. You can borrow one field and change another at the same time:
struct Player {
name: String,
score: u32,
}
fn main() {
let mut p = Player {
name: String::from("Ada"),
score: 10,
};
let name = &p.name;
p.score += 1;
println!("{name} has {}", p.score);
}This compiles because &p.name and the write to p.score touch different fields. A method call behaves differently. p.add_point() with &mut self borrows the whole struct, so it conflicts with a live &p.name. If you hit that error, either do the field access directly or end the first borrow before calling the method.
Moving a field out
Reading a Copy field such as u32 copies it. Reading a non-Copy field such as String by value moves it out of the struct:
struct Player {
name: String,
score: u32,
}
fn main() {
let p = Player {
name: String::from("Ada"),
score: 10,
};
let name = p.name;
println!("{} {}", name, p.score);
}After the move, p.name is unusable and so is p as a whole. But p.score is still readable, because only the name field was moved. This is called a partial move. If you want to keep the struct intact, borrow with &p.name or call .clone().
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `cannot assign to p.score, as p is not declared as mutable` | Binding lacks `mut` | Use `let mut p` |
| `cannot assign ... behind a & reference` | Parameter is `&Player` | Take `&mut Player` |
| `borrow of partially moved value` | Used `p` after moving a field out | Borrow or clone the field |
| `cannot borrow p as mutable because it is also borrowed as immutable` | A method call overlaps a live field borrow | Shorten the borrow or access fields directly |
Exercise
Given let p = Player { name: String::from("Ada"), score: 10 };, you write let n = p.name; and then println!("{}", p.score);. Does this compile? What if the next line is println!("{}", p.name);?
Show answer
The first version compiles. name is moved out (a partial move), but score is a u32, which is Copy, and it was not moved, so it is still readable. Adding println!("{}", p.name); fails, because p.name was moved into n and can no longer be used.