Introduction
A trait method does not always need to be implemented by every type. Rust allows a trait to provide a default implementation directly in the trait definition, giving implementers a ready-made behavior they can use unchanged or override with something more specific. This reduces boilerplate when many implementers share the same reasonable default while still allowing customization where needed.
Cricket analogy: A trait's default method is like a standard pre-match warm-up routine every team can follow as-is, but a team with a specialist fitness coach can override it with their own custom drill, reducing the need for every team to invent a routine from scratch.
Syntax
trait Summary {
fn summarize(&self) -> String {
String::from("(Read more...)")
}
}
struct Article {
title: String,
}
impl Summary for Article {}Explanation
In this example, Summary::summarize has a default body returning a placeholder string. Because Article has an empty impl Summary for Article {} block, it inherits the default behavior without writing its own summarize method. Any implementer can instead override the method by defining its own summarize inside its impl block, which takes precedence over the default. Default methods can also call other required (non-default) methods on self, letting a trait build higher-level behavior on top of a smaller set of methods each implementer must supply.
Cricket analogy: Summary::summarize returning a placeholder is like a league's default post-match quote template ('A hard-fought game') any team can use as-is; since Article's impl block is empty, it uses that stock quote, but a star player's impl could override it with a real personalized quote, and the default can still reference required stats like runs and wickets that every team must report.
Example
trait Summary {
fn summarize_author(&self) -> String;
fn summarize(&self) -> String {
format!("(Read more from {}...)", self.summarize_author())
}
}
struct Tweet {
username: String,
}
impl Summary for Tweet {
fn summarize_author(&self) -> String {
format!("@{}", self.username)
}
}
fn main() {
let tweet = Tweet { username: String::from("rustlang") };
println!("{}", tweet.summarize());
}Output
(Read more from @rustlang...)Key Takeaways
- A trait method can supply a default body directly in the trait definition.
- An empty
impl Trait for Type {}block uses the trait's default implementations unchanged. - Implementers can override a default method simply by defining their own version in the impl block.
- Default methods can call other required methods on self, layering behavior on a smaller required interface.
- Default implementations reduce boilerplate while still allowing per-type customization.
Practice what you learned
1. What happens when a trait method has a default implementation and a type's impl block is empty?
2. How does an implementer override a trait's default method?
3. Can a default trait method call other methods that don't have default implementations?
4. What is a key benefit of default trait implementations?
5. In the Summary trait example with summarize_author, why does Tweet need to implement summarize_author but not summarize?
Was this page helpful?
You May Also Like
Traits in Rust
Traits define shared behavior that types can implement, similar to interfaces in other languages.
Trait Objects and Dynamic Dispatch in Rust
Trait objects like `dyn Trait` enable runtime polymorphism for heterogeneous collections, at the cost of dynamic dispatch.
Generics in Rust
Generics let you write functions, structs, and enums that work over many types while remaining zero-cost at runtime.
Structs in Rust
Structs let you group related values into a single named type with fields you access by name.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics