---
title: "Discussion on \"Variables And Mutability In Rust\" | Hashnode"
description: "Discussion on \"Variables And Mutability In Rust\". In the last article, we talked about how to use Cargo for Rust projects and highlighted some of its most common commands. We are now going to go further and start talking about variables and mutability. \nOne of Rust's main features is that variables ..."
url: "https://hashnode.com/posts/variables-and-mutability/5f65008167d51d3b5d47980b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Variables And Mutability In Rust](https://edfloreshz.hashnode.dev/variables-and-mutability?utm_source=hashnode&utm_medium=feed)

In the last article, we talked about how to use Cargo for Rust projects and highlighted some of its most common commands. We are now going to go further and start talking about variables and mutability. One of Rust's main features is that variables ...

Sep 18, 2020 · 6 min read · edfloreshz.hashnode.dev  
Read the article

-   [#programming-ciovqvfcb008mb253jrczo9ye](https://hashnode.com/tag/programming-ciovqvfcb008mb253jrczo9ye)
-   [#beginners](https://hashnode.com/tag/beginners)
-   [#rust](https://hashnode.com/tag/rust)

## Written by

[Eduardo Flores](https://hashnode.com/@edfloreshz)

@edfloreshz

  
[View profile](https://hashnode.com/@edfloreshz)

Rustacean 🦀

I’m a software engineer very enthusiastic about Rust.

## Responses 2

Markdown supported · ⌘↵ to post

[Mark](https://hashnode.com/@mverleg)

[Sep 19, 2020](https://hashnode.com/posts/variables-and-mutability/5f65008167d51d3b5d47980b/comment/5f6625bb35d6463d2b5f8402)

A tricky part about Rust's immutability is this:

```
fn f() {
    let list = Vec::new();
    g(list);
}

fn g(mut list: Vec<i32>) {
    list.push(1);
    list = Vec::new();
}
```

We're changing the list that was immutable.

Maybe this seems weird for several reasons: 1) wasn't the list an immutable object? 2) why doesn't that work with borrows? 3) why does Rust let me change immutable to mutable?

The first thing to remember is that, just like other languages such as Java, it's the _reference_ that is immutable, not the object itself. Just, unlike Java, this also applies recursively to anything within the reference.

Another thing to remember is that, if you pass an object to another method, you're either passing sole ownership, or you're sending a copy.

If function `f` passes sole ownership of the `list` to `g`, then `f` cannot have any references anymore. Whatever `g` does, it won't affect `f`. So it's no problem if `g` makes it mutable.

And if `f` passes a copy, of course it won't care what `g` does with that; it still has it's own version.

[Mark](https://hashnode.com/@mverleg)

[Sep 19, 2020](https://hashnode.com/posts/variables-and-mutability/5f65008167d51d3b5d47980b/comment/5f66238e6dfc523d0a893514)

I think it's useful to highlight that this is a much stronger type of immutability than e.g. Java's `final`, or equivalents in many languages.

If in Java we make a list:

```
final var list = new ArrayList<Integer>();
```

we cannot reassign it:

```
list = new ArrayList<Integer>();
```

but we can change it just fine in other ways:

```
list.add(1);
list.add(2);
list.clear();
```

Rust's `Vec` is a quite similar datatype Java's `ArrayList`:

```
let list = Vec::new();
```

but we cannot change it at all:

```
list.push(1);
list.push(2);
list.clear();
```

gives:

```
error[E0596]: cannot borrow `list` as mutable, as it is not declared as mutable
```

We might say that Java (and most others) has shallow immutability, while Rust's is deep.

There are some caveats though, mostly `RefCell`, `Mutex` and `RwLock`.

[Eduardo Flores](https://hashnode.com/@edfloreshz)

[Sep 19, 2020](https://hashnode.com/posts/variables-and-mutability/5f65008167d51d3b5d47980b/comment/5f66238e6dfc523d0a893514#reply-5f664cfa35d6463d2b5f866f)

Thanks for your comments, I will make an article about smart pointers in the future where I will talk more about mutability.
