DEV Community

Cover image for Rust Regular References With The Deref Trait
Mansoor Ahmed
Mansoor Ahmed

Posted on

Rust Regular References With The Deref Trait

Introduction
Implementing the Deref trait permits us to customize the behavior of the dereference operator, * (as against the multiplication or glob operator). By implementing Deref in such how that a wise pointer is often treated kind of a daily reference, we’ll write code that operates on references and use that code with smart pointers too.

Description
Let’s first inspect how the to dereference operator works with regular references. Then secondly we’ll attempt to define a custom type that acts like Box, and see why the to dereference operator doesn’t work as a reference on our newly defined type. We’ll explore how implementing the Deref trait makes it possible for smart tips that would add ways almost like references. Then definitely we’ll inspect Rust’s deref coercion feature and therefore the way it lets us work with either references or smart pointers.

Note: There’s one big difference between the MyBox type we’re on the brink of build and thus the important Box: our version won’t store its data on the heap. We are focusing this instance on Deref, so where the data is basically stored may be a smaller amount important than the pointer-like behavior.

Following the Pointer to the price with the Dereference Operator
A regular reference could also be a kind of pointer, and a way to think about a pointer is as an arrow to a worth stored elsewhere. We’ll create a reference to an i32 value then use the dereference operator to follow the reference to the data:

Filename: src/main.rs

fn main() {
let x = 5;
let y = &x;

assert_eq!(5, x);
assert_eq!(5, *y);
}
Using the dereference operator to follow reference to an i32 value

The variable x holds an i32 value, 5. We set y capable reference to x. we’ll assert that x is capable 5. However, if we might wish to form an assertion about the price in y, we’ve to use *y to follow the reference to the price it’s pointing to (hence dereference). Once we dereference y, we’ve access to the integer value y is pointing thereto we’ll compare with 5.

If we tried to write down down assert_eq!(5, y); instead, we’d get this compilation error:

$ cargo run
Compiling deref-example v0.1.0 (file:///projects/deref-example)
error[E0277]: can’t compare {integer} with “
–> src/main.rs:6:5
|
6 | assert_eq!(5, y);
| ^^^^^^^^^^^^^^^^^ no implementation for {integer} ==
|
= help: the trait PartialEq isn’t implemented for {integer}
= note: Run with -Z macro-backtrace for more info this error originates during a macro (in Nightly builds, )

error: aborting because of previous error

Try rustc –explain E0277for more information about this error.
error: couldn’t compile deref-example

Run the command again with –verbose to learn more. Comparing variety and reference to variety isn’t allowed because they’re differing kinds . We must use the dereference operator to follow the reference to the price it’s pointing to.

Using Box kind of a Reference
We can rewrite the code to use a Box instead of a reference; the dereference operator will work as:

Filename: src/main.rs
fn main() {
let x = 5;
let y = Box::new(x);

assert_eq!(5, x);
assert_eq!(5, *y);
}
Using the dereference operator on a Box

The only difference is that here we set y to be an instance of a box pointing to a copied value of x rather than a reference pointing to the price of x. within the last assertion, we’ll use the dereference, operator, to follow the box’s pointer within an equivalent way that we did when y was a reference. Next, we’ll explore what’s special about Box that allows us to use the dereference operator by defining our own box type.
For more details visit:https://www.technologiesinindustry4.com/2021/05/rust-regular-references-with-the-deref-trait.html

Discussion (0)