[Rust Basics] Rust Dereference

preface

In Rust, dereferencing refers to using *the operator to access a value inside a pointer or smart pointer. Dereferencing allows us to get the actual value pointed to by the pointer so we can read or modify it. This blog will introduce in detail the usage and related concepts of dereferencing in Rust.

dereference operator

In Rust, the dereference operator *is used to get the actual value from a pointer or smart pointer. By dereferencing, we can access the memory location pointed by the pointer and read or modify it.

To dereference, we need to prefix the pointer with the operator *. Here's an example that demonstrates how to use the dereference operator:

fn main() {
    
    
    let x = 5;
    let ptr = &x; // 创建一个指向 x 的引用
    let value = *ptr; // 解引用获取 x 的值
    println!("value = {}", value);
}

In the above example, we created a reference to the integer x, ptrthen *ptrdereferenced it using to get xthe value of and assigned it to valuethe variable. Finally, we print valuethe value of .

Dereference and Ownership Transfer

In Rust, dereferencing does not trigger ownership transfer. Dereferencing simply takes the value in a pointer or smart pointer and does not change its ownership.

For example, with Boxsmart pointers, the dereference operation simply returns the value contained in the pointer and does not transfer ownership. Here's an example that demonstrates a situation where knowing a reference doesn't trigger a transfer of ownership:

fn main() {
    
    
    let x = Box::new(5);
    let value = *x; // 解引用获取 x 的值
    println!("value = {}", value);
}

In the above example, we created a Boxsmart pointer xand then used to *xdereference to get xthe value contained in . Since dereferencing does not trigger an ownership transfer, xownership of the remains valid.

Dereferencing and mutability

In Rust, the dereference operation can determine whether the value pointed to by the pointer can be modified according to the mutability of the context. For immutable references, the dereference operation can only be used to read the value; for mutable references, the dereference operation can read and modify the value.

Here's an example that demonstrates how understanding references behave differently with respect to mutability:

fn main() {
    
    
    let mut x = 5;
    let ptr = &mut x; // 创建一个可变引用
    *ptr = 10; // 解引用并修改值
    println!("x = {}", x);
}

In the above example, we created a mutable reference ptr, then used *ptrto dereference and modify the value. Due to the presence of mutable references, dereference operations can modify xthe value of .

Dereference and borrow checker

In Rust, dereferencing is controlled by the borrow checker. The borrow checker statically analyzes code at compile time to ensure that dereference operations do not cause problems such as dangling pointers or data races.

If the dereference operation violates the borrowing rules, the compiler will report an error. For example, attempting to create multiple immutable references at the same time as mutable references will result in a compilation error.

fn main() {
    
    
    let mut x = 5;
    let ptr = &mut x; // 创建一个可变引用
    let value = *ptr; // 错误!不可同时存在可变和不可变引用
    println!("value = {}", value);
}

In the above example, we ptrattempted to create an immutable reference while a mutable reference existed value, which would violate the borrowing rules and result in a compilation error.

Summarize

This blog introduces in detail the usage and related concepts of dereferencing in Rust. Dereferencing allows us to access the actual value in a pointer or smart pointer and decide whether it can be modified based on the mutability of the context.

I hope this blog helps you understand and apply dereference operations in Rust. Thanks for reading!

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/131744296