Skip to Content
E0594: Cannot Assign to Field

error[E0594]: cannot assign, as it is not declared as mutable

You assigned to a field of a struct that isn’t mutable. In Rust, mutability belongs to the whole binding, never to individual fields.

Quick fix: Declare the struct variable with let mut, or take &mut instead of & in the function that changes it.

Code that triggers E0594

src/main.rs
struct Player { name: String, score: u32, } fn main() { let player = Player { name: String::from("Ferris"), score: 0 }; player.score = 10; println!("{} has {} points", player.name, player.score); }

What the compiler says

error[E0594]: cannot assign to `player.score`, as `player` is not declared as mutable --> src/main.rs:8:5 | 8 | player.score = 10; | ^^^^^^^^^^^^^^^^^ cannot assign | help: consider changing this to be mutable | 7 | let mut player = Player { name: String::from("Ferris"), score: 0 }; | +++ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0594`.

Why Rust raises E0594

player was declared without mut, so none of its fields can be assigned. Rust has no way to mark just one field as mutable: either the whole value can change through this binding, or nothing can.

The same error appears inside a function that receives a shared reference. Here reset gets a read-only &Player:

src/main.rs
struct Player { name: String, score: u32, } fn reset(player: &Player) { player.score = 0; } fn main() { let player = Player { name: String::from("Ferris"), score: 10 }; reset(&player); println!("{} has {} points", player.name, player.score); }
error[E0594]: cannot assign to `player.score`, which is behind a `&` reference --> src/main.rs:7:5 | 7 | player.score = 0; | ^^^^^^^^^^^^^^^^ `player` is a `&` reference, so the data it refers to cannot be written | help: consider changing this to be a mutable reference | 6 | fn reset(player: &mut Player) { | +++ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0594`.

How to fix E0594

1. Make the binding mutable

Add mut where the struct variable is declared.

src/main.rs
struct Player { name: String, score: u32, } fn main() { let mut player = Player { name: String::from("Ferris"), score: 0 }; player.score = 10; println!("{} has {} points", player.name, player.score); }

2. Pass &mut when a function changes the struct

Change the parameter to &mut Player and pass &mut player. Methods that change fields take &mut self for the same reason.

src/main.rs
struct Player { name: String, score: u32, } fn reset(player: &mut Player) { player.score = 0; } fn main() { let mut player = Player { name: String::from("Ferris"), score: 10 }; reset(&mut player); println!("{} has {} points", player.name, player.score); }

Learn the concept behind it

The Structs lesson explains the rules behind E0594 with diagrams and more examples. For the official reference, run rustc --explain E0594 or read the E0594 entry in the Rust error index .

Hit a different error? Paste it into the Rust error lookup to find its fix.

Last updated on