Skip to content

Compiler-Hint to assign to a mutably borrowed value #136026

Closed as duplicate of#136028
@ValonPrime

Description

@ValonPrime

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;
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:9:11
  |
9 |     val = &10;
  |           ^^^ types differ in mutability
  |
  = note: expected mutable reference `&mut _`
                     found reference `&_`

Desired output

error[E0308]: mismatched types
 --> src/main.rs:9:11
  |
9 |     val = &10;
  |           ^^^ types differ in mutability
  |
  = note: expected mutable reference `&mut _`
                     found reference `&_`
help: consider dereferencing here to assign to the mutably borrowed value
  |
9 |     *val = 10;
  |     +

Rationale and extra context

A beginner might add a &-Symbol by themself, because val is a (mutable) reference. The output suggest adding a mut. However, assigning a mutably borrowed value might be desired in this case.

Internals: https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209

Other cases

If you follow the current output and add the mut, one receives a Error about a too short lifetime.
I suggest the same hint in that case as well.

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`

and similar, when using a variable instead of a constant:

fn change_val(mut val: &mut i32) {
    let mut new_val = 10;
    val = &mut new_val;
}
error[E0597]: `new_val` does not live long enough
  --> src/main.rs:10:11
   |
8  | fn change_val(mut val: &mut i32) {
   |                        - let's call the lifetime of this reference `'1`
9  |     let mut new_val = 10;
   |         ----------- binding `new_val` declared here
10 |     val = &mut new_val;
   |     ------^^^^^^^^^^^^
   |     |     |
   |     |     borrowed value does not live long enough
   |     assignment requires that `new_val` is borrowed for `'1`
11 | }
   | - `new_val` dropped here while still borrowed

Rust Version

valon@valon:~/RustroverProjects/future_test$ rustc --version --verbose
rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions