mun icon indicating copy to clipboard operation
mun copied to clipboard

Variable mutability

Open Wodann opened this issue 5 years ago • 0 comments

Similar to Rust, we want to use opt-in mutability; i.e. by default all variables are immutable. If you want to edit a variable, you need to declare it as with the mut keyword. E.g. this example should result in a compile error:

let foo = 5;
if condition() {
  foo += 1; // error: `foo` is not mutable
}

This code would be correct:

let mut foo = 5;
if condition() {
  foo += 1;
}

This construct extends to function arguments. E.g. if you want to make a function argument mutable:

fn foo(mut arg: u32) {
  arg += 1;
  // ...
}

Wodann avatar May 22 '20 15:05 Wodann