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

Mutability - Rust by Example

In Rust, variable bindings are immutable by default, but can be made mutable using the 'mut' modifier. The document provides an example demonstrating how to mutate a variable while highlighting that attempting to change an immutable variable results in a compiler error. Detailed diagnostics are provided by the compiler for mutability errors.

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)
28 views1 page

Mutability - Rust by Example

In Rust, variable bindings are immutable by default, but can be made mutable using the 'mut' modifier. The document provides an example demonstrating how to mutate a variable while highlighting that attempting to change an immutable variable results in a compiler error. Detailed diagnostics are provided by the compiler for mutability errors.

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