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
Enables wrapper types to expose an inner value’s methods without making the inner field public.
How deep it goes
Works through a Deref implementation, method calls, and compiler errors, then distinguishes immutable dereferencing from DerefMut.
Examples
Uses a number-holding tuple struct, Box comparison, checked subtraction, and a confusing game-character implementation.
What you can do after
Build it from scratch for a simple wrapper, access its target’s methods, and recognize when named methods are clearer.

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 →

Related concepts

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.