gotcharustCritical
What's the difference between placing "mut" before a variable name and after the ":"?
Viewed 0 times
nameandbetweenwhatthedifferencevariablemutbeforeafter
Problem
Here are two function signatures I saw in the Rust documentation:
Why the different placement of
It seems that the first function could also be declared as
fn modify_foo(mut foo: Box) { *foo += 1; *foo }
fn modify_foo(foo: &mut i32) { *foo += 1; *foo }Why the different placement of
mut?It seems that the first function could also be declared as
fn modify_foo(foo: mut Box) { /* ... */ }Solution
mut foo: T means you have a variable called foo that is a T. You are allowed to change what the variable refers to:let mut val1 = 2;
val1 = 3; // OK
let val2 = 2;
val2 = 3; // error: re-assignment of immutable variableThis also lets you modify fields of a struct that you own:
struct Monster { health: u8 }
let mut orc = Monster { health: 93 };
orc.health -= 54;
let goblin = Monster { health: 28 };
goblin.health += 10; // error: cannot assign to immutable fieldfoo: &mut T means you have a variable that refers to (&) a value and you are allowed to change (mut) the referred value (including fields, if it is a struct):let val1 = &mut 2;
*val1 = 3; // OK
let val2 = &2;
*val2 = 3; // error: cannot assign to immutable borrowed contentNote that
&mut only makes sense with a reference - foo: mut T is not valid syntax. You can also combine the two qualifiers (let mut a: &mut T), when it makes sense.Code Snippets
let mut val1 = 2;
val1 = 3; // OK
let val2 = 2;
val2 = 3; // error: re-assignment of immutable variablestruct Monster { health: u8 }
let mut orc = Monster { health: 93 };
orc.health -= 54;
let goblin = Monster { health: 28 };
goblin.health += 10; // error: cannot assign to immutable fieldlet val1 = &mut 2;
*val1 = 3; // OK
let val2 = &2;
*val2 = 3; // error: cannot assign to immutable borrowed contentContext
Stack Overflow Q#28587698, score: 148
Revisions (0)
No revisions yet.