Rust Traits Cheat Sheet
Explains how to define and implement traits, use trait bounds and generics, default methods, and trait objects for dynamic dispatch in Rust.
2 PagesIntermediateMar 22, 2026
Defining & Implementing
Declaring a trait and implementing it for a type.
rust
trait Summary { fn summarize(&self) -> String;}struct Article { title: String, body: String,}impl Summary for Article { fn summarize(&self) -> String { format!("{}: {}...", self.title, &self.body[..20.min(self.body.len())]) }}let a = Article { title: "Rust".into(), body: "Traits are powerful".into() };println!("{}", a.summarize());
Default Methods
Traits can provide implementations that types inherit for free.
rust
trait Greet { fn name(&self) -> String; // Default implementation; can be overridden fn greet(&self) -> String { format!("Hello, {}!", self.name()) }}struct Person { name: String }impl Greet for Person { fn name(&self) -> String { self.name.clone() } // uses default greet()}
Trait Bounds
Constraining generic parameters to types that implement a trait.
rust
// Trait bound with `impl Trait` syntaxfn notify(item: &impl Summary) { println!("Breaking news! {}", item.summarize());}// Equivalent, explicit generic syntaxfn notify_generic<T: Summary>(item: &T) { println!("Breaking news! {}", item.summarize());}// Multiple bounds with `+`fn print_summary<T: Summary + std::fmt::Debug>(item: &T) { println!("{:?}", item);}// `where` clause for readability with many boundsfn complex<T, U>(t: &T, u: &U) -> Stringwhere T: Summary, U: Clone + std::fmt::Debug,{ t.summarize()}
Trait Objects
Dynamic dispatch for heterogeneous collections.
rust
// dyn Trait enables dynamic dispatch; size not known at compile timefn notify_dyn(item: &dyn Summary) { println!("News: {}", item.summarize());}// A Vec of trait objects, boxed on the heaplet items: Vec<Box<dyn Summary>> = vec![ Box::new(Article { title: "A".into(), body: "First".into() }), Box::new(Article { title: "B".into(), body: "Second".into() }),];for item in &items { println!("{}", item.summarize());}
Common Derivable Traits
Traits the compiler can auto-implement with #[derive(...)].
- Debug- #[derive(Debug)] enables {:?} formatting for a type
- Clone- #[derive(Clone)] adds a .clone() method that deep-copies the value
- Copy- #[derive(Copy)] makes the type implicitly copied instead of moved (requires Clone)
- PartialEq / Eq- Enables == and != comparisons between instances
- PartialOrd / Ord- Enables <, >, and sorting via comparison
- Default- #[derive(Default)] provides a Default::default() constructor
- Hash- Allows the type to be used as a HashMap/HashSet key
Pro Tip
Use `impl Trait` in argument and return position for zero-cost static dispatch when the concrete type is known at compile time; reach for `Box<dyn Trait>` only when you need a heterogeneous collection or genuinely dynamic dispatch.
Was this cheat sheet helpful?
Explore Topics
#RustTraits#RustTraitsCheatSheet#Programming#Intermediate#DefiningImplementing#DefaultMethods#TraitBounds#TraitObjects#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance