Mutability - Rust by Example
Mutability - Rust by Example
2023 12:57
Mutability
Variable bindings are immutable by default, but this can be overridden using the mut
modifier.
1 fn main() {
2 let _immutable_binding = 1;
3 let mut mutable_binding = 1;
4
5 println!("Before mutation: {}", mutable_binding);
6
7 // Ok
8 mutable_binding += 1;
9
10 println!("After mutation: {}", mutable_binding);
11
12 // Error! Cannot assign a new value to an immutable variable
13 _immutable_binding += 1;
14 }
https://doc.rust-lang.org/rust-by-example/variable_bindings/mut.html Stránka 1 z 1