0% found this document useful (0 votes)
21 views1 page

Mutability - Rust by Example

Uploaded by

Vladimír Pilát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Mutability - Rust by Example

Uploaded by

Vladimír Pilát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Mutability - Rust By Example 09.10.

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 }

The compiler will throw a detailed diagnostic about mutability errors.

https://doc.rust-lang.org/rust-by-example/variable_bindings/mut.html Stránka 1 z 1

You might also like