liveBook · Manning

Deref

Deref is a Rust standard-library trait that defines how a value is dereferenced to access a target value and enables dereference coercions.

How 2 Manning books explain it

Learn Rust in a Month of Lunches

Deref is the Rust standard trait that defines immutable dereferencing and enables automatic dereference coercions for method and field access.

How it is used
Introduces Deref as a way for wrapper types to expose dereferencing and access methods of an inner type.
How deep it goes
Works through a Deref implementation, compiler errors, target methods, and the distinction between immutable and mutable dereferencing.
Examples
Uses a number wrapper with checked subtraction and a game character whose hit points illustrate confusing dereferencing.
What you can do after
Build a Deref implementation for a wrapper and evaluate whether dereferencing makes its behavior clear.

Read how Learn Rust in a Month of Lunches explains Deref →

Idiomatic Rust

Deref is a Rust trait that defines how a value can be dereferenced to access a target value through the dereference operator.

How it is used
Enables wrapper structs and smart pointers to expose target methods through dereference coercion.
How deep it goes
Works through a wrapper implementation and method-call behavior, then identifies consuming-method and mutable-access limits.
Examples
Uses a wrapped vector’s `iter()` and `into_iter()` calls, plus a person wrapper whose `len()` call is misleading.
What you can do after
Apply it via Rust’s `Deref` trait to expose appropriate wrapper methods and choose explicit methods or traits when coercion obscures intent.

Read how Idiomatic Rust explains Deref →

Also covered in

What to know first

Related concepts

Questions about Deref

What is Deref?

Deref is a Rust standard-library trait that defines how a value is dereferenced to access a target value and enables dereference coercions.

Which Manning books explain Deref?

Learn Rust in a Month of Lunches and Idiomatic Rust.

What should I know before learning Deref?

Manning books that teach Deref usually introduce borrow checker, inheritance, method and option first.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.