# Ref mut vs &mut vs mut &

**URL:** <https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049>\
**Category:** uncategorized\
**Created:** [January 29, 2024, 9:58pm UTC](https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049 "2024-01-29T21:58:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alphapage](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alphapage/32/14613_2.png) [@Alphapage](https://users.rust-lang.org/u/Alphapage)\
**Post date:** [January 29, 2024, 9:58pm UTC](https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049/1 "2024-01-29T21:58:14Z")

</div>

Hello,

In my opinion, there is no difference between those functions:

```rust
fn fun1(ref mut a: i32) {
    *a = 1;
    let mut b = 0;
    fun1(b)
}
fn fun2(a: &mut i32) {
    *a = 1;
    let mut b = 0;
    fun2(&mut b)
}
fn fun3(mut a: &i32) {
    *a = 1; // error
    let mut b = 0;
    fun3(&b)
}

```

Except the last one which doesn't compile because `a` is behind `&` reference, but `a` is `mut` !!!

Thank you in advance for your answers.

---

<div class="post-metadata">

**Author:** ![drewtato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/drewtato/32/32949_2.png) [@drewtato](https://users.rust-lang.org/u/drewtato)\
**Post date:** [January 29, 2024, 10:10pm UTC](https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049/2 "2024-01-29T22:10:18Z")

</div>

What's your question? They are all different.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 29, 2024, 10:19pm UTC](https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049/3 "2024-01-29T22:19:15Z")

</div>

`fun1` desugars as follows:

The pattern `ref mut a` is matched against the actual argument, so this is like

```rust
fn fun1(mut argument: i32) {
    let ref mut a = argument;
    *a = 1;
    let mut b = 0;
    fun1(b)
}

```

Then, the `ref mut a` patterns means that the value is bound by mutable reference, so this is equivalent to

```rust
fn fun1(mut argument: i32) {
    let a = &mut argument;
    *a = 1;
    let mut b = 0;
    fun1(b)
}

```

Finally, to avoid any confusion, we can remove the usage of `mut` pattern in the argument by doing an additional copy/move step (in this case it’s a copy), which means you best think of your function as doing

```rust
fn fun1(arg: i32) {
    let mut var = arg;
    let a = &mut var;
    *a = 1;
    let mut b = 0;
    fun1(b)
}

```

In comparison to `fun2` you thus see, it takes a `i32` _by value_, stores it in a _local_ mutable variable _inside_ of the function, and furthermore mutations, as done with the `*a = 1` only apply to that _local copy_ of the value.

This means that a function like `fun1` (ignoring the infinite recursion business here…) will _not_ mutate the variable you pass, on the call-site, whereas a function like `fun2` – taking a `&mut i32` argument – would!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 28, 2024, 10:20pm UTC](https://users.rust-lang.org/t/ref-mut-vs-mut-vs-mut/106049/4 "2024-04-28T22:20:00Z")

</div>

Closed on April 28, 2024, 10:20pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
