A beginner might try the following code:
fn main() {
let mut some_val = 5;
change_val(&mut some_val);
println!("{}", some_val);
}
fn change_val(mut val: &mut i32) {
val = &10;
}
error[E0308]: mismatched types
--> src/main.rs:9:11
|
9 | val = &10;
| ^^^ types differ in mutability
|
= note: expected mutable reference `&mut _`
found reference `&_`
val
needs to be a (mutable) reference, so the 10 is made a reference by using a &-Symbol in front of it.
However this is not how you assign through a reference in this case but the compiler only suggest you to add a mut
:
fn change_val(mut val: &mut i32) {
val = &mut 10;
}
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:9:16
|
8 | fn change_val(mut val: &mut i32) {
| - let's call the lifetime of this reference `'1`
9 | val = &mut 10;
| -----------^^- temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| assignment requires that borrow lasts for `'1`
This creates another, not working, piece of code. Now val
doesn't live long enough.
Parallel to the errors, the compiler should hint you to assign through the reference, like with this implementation:
fn main() {
let mut some_val = 5;
change_val(&mut some_val);
println!("{}", some_val);
}
fn change_val(mut val: &mut i32) {
val = 10;
}
error[E0308]: mismatched types
--> src/main.rs:9:11
|
8 | fn change_val(mut val: &mut i32) {
| -------- expected due to this parameter type
9 | val = 10;
| ^^ expected `&mut i32`, found integer
|
help: consider dereferencing here to assign to the mutably borrowed value
|
9 | *val = 10;
| +
The current status can be confusing and frustrating for rust-beginners. Especially if they come from languages like Java. A hint like this could help beginners.