Search⌘ K

Deref

Explore how to work with mutable references in Rust by using the deref operator. Understand why direct operations on references fail and learn to correctly dereference to modify values. This lesson helps you fix common errors and better manage memory through references.

We'll cover the following...

Alright, I’m tired of fruit. Let’s talk about plain old numbers again. Consider this program that only uses mutable variables, no references:

Rust 1.40.0
fn main() {
let x: i32 = 5;
let mut y: i32 = 6;
y -= 1;
assert_eq!(x, y);
println!("Success");
}

What this means is: create a new immutable variable, x, that holds 5. Then create a mutable variable, y, that holds 6. Subtract 1 from y ...