mun
mun copied to clipboard
Variable mutability
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;
// ...
}