Deref
Deref is a Rust standard-library trait that defines how a value is dereferenced to access a target value and enables dereference coercions.
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.
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.
Also covered in
What to know first
- borrow checkerA borrow checker is a compiler component that statically enforces ownership, borrowing, and reference-lifetime rules to prevent invalid memory access.
- inheritanceInheritance is a mechanism in which a derived type or descendant element receives properties or behavior from a parent, subject to rules that may allow specialization or overriding.
- methodA method is a callable function or operation associated with a type, class, or object and invoked through that association.
- optionAn option type is a type that represents either a value of a specified type or its absence as distinct alternatives.
Related concepts
- associated typeuses · An associated type is a type member declared by a trait or similar abstraction whose concrete type is specified by each implementation.
- Cowimplemented by · Cow is Rust’s standard-library clone-on-write type, representing either borrowed or owned data and creating owned data when necessary.
- deref coercionused by · Deref coercion is Rust's automatic conversion of a reference to a wrapper value into a reference to its dereference target when resolving method calls and compatible operations.
- smart pointerimplemented by · A smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
- polymorphismuses · Polymorphism is a programming-language capability that lets a common operation or interface work with values of different types, with behavior or implementation determined by the type or value.
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.

