Mutable Variables

Making a variable mutable is simple with the mut keyword. Mutability means that the value of a variable can be overwritten. See the example below:

fn main() {
  // the `mut` keyword means that 'x' is now able to be overwritten
  let mut x = 1;
  println!("The value of x is {x}");  // prints 1 as the value of x
  x = 2;
  println!("The value of x is {x}");  // prints 2 as the value of x
}

Leave a comment