development-programming-language-rust-traits.html
* created: 2025-07-17T12:02
* modified: 2025-07-17T12:51
title
Rust Traits
description
Traits are used to share functionality over multiple types. Furthermore we can define trait bounds that check if a type implements a certain trait and therefore has certain properties.
What are Traits?
Common Traits for Types
The following traits should generally be implemented by most of the types in a API:
- Debug, Clone
- Send, Sync and Unpin
- PartialEq, PartialOrd, Hash, Eq and Ord
- Serialize and Deserialize (as opt in)
When using wrapper types the following traits should be implemented in addition: Deref, AsRef, From/Into<InnerType>, Borrow.
Exception
Copy: People generally don't expect types to implement Copy because it changes the move semantics of a given type, which might surprise user. It is often advisable to rely on Clone instead to explicitly create a a new copy. In addition Copy is highly restrictive and could result in a breaking change if a type has to rely on something like String further done the line.