Sitemap
Rustaceans

Exploring Rust: Insights and Expertise from Rustaceans

Follow publication

Mastering the Trait in Rust: Patterns, Pitfalls, and Practical Use

5 min readJul 25, 2025
Press enter or click to view image in full size
Crabby unrwapping

One of the less visible but incredibly useful features of Rust is the trait. If you've used , , or , you've already relied on it, even if you've never written an implementation yourself. allows smart pointers and custom wrapper types to behave like references to their inner values. This enables ergonomic code, eliminates boilerplate, and ensures seamless integration with existing APIs.

Despite its simplicity, touches on important design principles. It encourages type safety, allows abstractions to remain lightweight, and ensures that custom types can blend into the broader Rust ecosystem without friction.

In this article, we’ll break down what actually does under the hood, discuss when and why to implement it, highlight common mistakes, and finish with a practical example that demonstrates its utility in a slightly more complex, real-world scenario.

What is the Trait?

The trait is part of Rust’s core mechanism for abstraction over indirection. It lets a type specify what it should be dereferenced to, which is particularly useful for smart pointers and wrapper types. The standard library uses to allow boxed and reference-counted types to behave like references to their contained…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Rustaceans

Published in Rustaceans

Exploring Rust: Insights and Expertise from Rustaceans

Responses (1)

Write a response

Do not use Deref to paper over differences in behavior.

Thanks for the article! It explains the topic very good and understandably! It should maybe be woven into the related „The Book“ chapter!

Regarding the highlighted text, a question bugged me lately: Would you consider it bad or good use of deref to…

5