Traits
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and traits are how it defines shared behaviour, the foundation of generics and polymorphism.
Learn Traits in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll define traits, implement them for your types, use default methods, and accept any implementer with impl Trait or dyn Trait .
What You'll Learn in This Lesson
1️⃣ Defining and Implementing Traits
A trait lists methods that implementing types must provide — and may include default implementations. You connect a trait to a type with impl Trait for Type , then call the methods just like any other.
Tweet didn't define preview , so it inherited the trait's default — which itself called the type's own summarize . That layering is what makes traits so composable.
2️⃣ Trait Bounds, impl Trait, and dyn Trait
Traits unlock polymorphism. Writing a parameter as &impl Greet accepts any type implementing Greet with fast static dispatch. To store different implementing types together, use a trait object Box<dyn Greet> , which uses dynamic dispatch.
The Vec<Box<dyn Greet>> held both an English and a Spanish value at once — impossible with a plain generic, which fixes a single type. That's the trade: dyn for flexibility, impl for speed.
Your turn. Fill in the blanks marked ___ , then run it.
Define a trait, implement it for two structs, and accept any implementer with impl Trait . Run it with cargo run .
📋 Quick Reference — Traits
Practice quiz
What does a trait define in Rust?
- A growable list
- A memory region
- Shared behaviour as a set of methods
- A new integer type
Answer: Shared behaviour as a set of methods. A trait declares methods that implementing types must provide, describing shared behaviour.
Which syntax implements a trait Summary for a type Article?
- impl Summary for Article { }
- impl Article: Summary { }
- trait Summary for Article { }
- Article impl Summary { }
Answer: impl Summary for Article { }. You connect a trait to a type with impl Trait for Type.
What is a default method in a trait?
- A required method with no body
- A private helper
- A method that returns Self
- A method with a body the trait provides automatically
Answer: A method with a body the trait provides automatically. A trait can supply a complete default body that implementors inherit unless they override it.
A type that uses a trait's default method without overriding it will...
- Fail to compile
- Get the trait's provided behaviour for free
- Return None
- Panic at runtime
Answer: Get the trait's provided behaviour for free. Default methods reduce boilerplate; you only must implement the required methods.
What does fn announce(g: &impl Greet) accept?
- Any type that implements Greet, via static dispatch
- Only the Greet trait itself
- Any type at all
- Only Box<dyn Greet>
Answer: Any type that implements Greet, via static dispatch. impl Trait in argument position accepts any implementor with fast static dispatch.
Which lets you store DIFFERENT implementing types together in one Vec?
- Vec<impl Greet>
- Vec<Greet>
- Vec<Box<dyn Greet>>
- Vec<&Greet>
Answer: Vec<Box<dyn Greet>>. Trait objects boxed as Vec<Box<dyn Greet>> can hold several concrete types at once.
impl Trait uses which kind of dispatch?
- Dynamic dispatch
- Static dispatch
- Runtime reflection
- Vtable lookup
Answer: Static dispatch. impl Trait is monomorphized like generics, resolving calls at compile time.
The orphan rule says you can implement a trait for a type only if...
- Both are from the standard library
- Neither is in your crate
- The type is a primitive
- The trait or the type is local to your crate
Answer: The trait or the type is local to your crate. At least one of the trait or the type must be defined in your own crate.
What does #[derive(Debug, Clone, PartialEq)] do?
- Deletes the type
- Implements those traits automatically
- Disables the traits
- Renames the struct
Answer: Implements those traits automatically. derive auto-generates standard trait implementations for your type.
Error 'not all trait items implemented' usually means...
- You added an extra method
- You used impl Trait
- You skipped a required method
- You forgot a lifetime
Answer: You skipped a required method. Every required method (one without a default) must be implemented.