Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 6, 2021 19:33
Show Gist options
  • Save rpivo/ceea4c36da1aea3f52d3e182f1294dd2 to your computer and use it in GitHub Desktop.
Save rpivo/ceea4c36da1aea3f52d3e182f1294dd2 to your computer and use it in GitHub Desktop.
Declaring Mutable & Immutable Variables in Rust

Declaring Mutable & Immutable Variables in Rust

We can declare mutable and immutable variables in Rust like so:

let foo = 0; // immutable
let mut bar = 1; // mutable

Note that immutability is the default. To make a variable mutable, we need to use the mut prefix.

fn main() {
    let foo = 0;
    let mut bar = 1;
    bar = 2; // okay to reassign
    foo = 3; // ERROR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment