# Compiler-Hint to assign to a mutably borrowed value

**URL:** <https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209>\
**Category:** compiler\
**Created:** [January 22, 2025, 7:20pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209 "2025-01-22T19:20:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ValonPrime](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/valonprime/32/12924_2.png) [@ValonPrime](https://internals.rust-lang.org/u/ValonPrime)\
**Post date:** [January 22, 2025, 7:20pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209/1 "2025-01-22T19:20:18Z")

</div>

A beginner might try the following code:

```rust
fn main() {
    let mut some_val = 5;
    change_val(&mut some_val);
    println!("{}", some_val);

}

fn change_val(mut val: &mut i32) {
    val = &10;
}

```

```rust
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`:

```rust
fn change_val(mut val: &mut i32) {
    val = &mut 10;
}

```

```rust
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:

```rust
fn main() {
    let mut some_val = 5;
    change_val(&mut some_val);
    println!("{}", some_val);

}

fn change_val(mut val: &mut i32) {
    val = 10;
}

```

```rust
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.

---

<div class="post-metadata">

**Author:** ![ekuber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ekuber/32/1646_2.png) [@ekuber](https://internals.rust-lang.org/u/ekuber)\
**Post date:** [January 22, 2025, 7:55pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209/2 "2025-01-22T19:55:18Z")

</div>

Note that after [https://github.com/rust-lang/rust/pull/134977](https://github.com/rust-lang/rust/pull/134977), if you match the mutability you get

```rust
warning: value assigned to `val` is never read
 --> src/main.rs:9:5
  |
9 | val = &mut 10;
  | ^^^
  |
  = note: `#[warn(unused_assignments)]` on by default
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
  |
8 ~ fn change_val(val: &mut mut i32) {
9 ~ *val = 10;
  |

warning: unused variable: `val`
 --> src/main.rs:8:19
  |
8 | fn change_val(mut val: &mut i32) {
  | ^^^ help: if this is intentional, prefix it with an underscore: `_val`
  |
  = note: `#[warn(unused_variables)]` on by default

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`

```

Note the suggestion in the lint.

* * *

Regardless, I agree it'd be nice to detect the case you mentioned. Can you file a ticket with the same content as your thread comment?

---

<div class="post-metadata">

**Author:** ![ValonPrime](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/valonprime/32/12924_2.png) [@ValonPrime](https://internals.rust-lang.org/u/ValonPrime)\
**Post date:** [January 24, 2025, 8:27pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209/3 "2025-01-24T20:27:32Z")

</div>

Thanks for your Input.

How exactly do I file a Ticket? Im being lead back here when I try to create a feature request on the github-repo.

Do I just use the blank template?

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [January 24, 2025, 9:00pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209/4 "2025-01-24T21:00:34Z")

</div>

"New Issue" then "Diagnostic Issue" will bring up [this form](https://github.com/rust-lang/rust/issues/new?template=diagnostics.yaml)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 18, 2026, 9:01pm UTC](https://internals.rust-lang.org/t/compiler-hint-to-assign-to-a-mutably-borrowed-value/22209/5 "2026-07-18T21:01:34Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
