April 2026
Intermediate
631 pages
16h 20m
English
Dereferencing is the process of accessing the value pointed to by a reference or pointer. This concept is closely related to borrowing because when you borrow a reference to data, you often need to dereference it to manipulate or access the underlying value. Let’s cover the basics of dereferencing through the example shown in Listing 4.26.
fn main() { let mut some_data = 42; let ref1 = &mut some_data; let deref_copy = *ref1; *ref1 = 13; println!("some_data is: {some_data}, deref_copy is: {deref_copy}");}
Listing 4.26 Dereferencing Stack-Allocated Data behind a Mutable Reference
In this case, we have a mutable reference ref1 to some_data. The line let deref_copy = *ref1 essentially does two things. First, it dereferences ...
Read now
Unlock full access