
Understand it
A trait may provide a method body that implementations inherit. Defaults reduce repetition, while required methods still define the minimum contract each type must supply.
A default method supplies behavior that implementers may inherit. In Summary, each type must provide title(), while summary() can be built from that required method. A new type gets a useful summary immediately after implementing the minimal contract. It may override the default when its presentation needs more context, but callers can still rely on the trait method existing.
This is a way to centralize a policy that is genuinely shared. If the default silently makes assumptions that are false for some types, it can produce misleading output. Keep required methods small and explicit, and make the default's dependency on them visible. For example, summary() calls self.title() so the implementation's title is always reflected. If the trait grows a new required method, every implementer must update; if it grows a default method, existing implementers usually continue to compile.
Create two structs implementing Summary: one inherits summary(), and the other overrides it. Call both through a generic function constrained by Summary and compare output. Then imagine a third type with no meaningful title. That is a sign the trait may be the wrong abstraction for that type, even if a placeholder string would make it compile. Good defaults reduce repetition while preserving an honest contract for every implementation.
Read the code
trait Summary {
fn title(&self) -> &str;
fn summary(&self) -> String {
format!("Title: {}", self.title())
}
}The example makes the contract visible in code.
Try it
Say which method of Summary an impl must write, and which it inherits.
What success looks like: You can write a minimal impl, and explain what summary() returns for it.
Check your understanding
Which method must an impl provide?
Reveal the answer
- title
- summary
- Both methods
Correct: title.
Go deeper
Common misconception: “A type must write every method a trait declares, including summary.”
Only methods with no body are required. An impl must provide title, while summary is inherited from its default body and can still be overridden.
- Sort the methods:
titleends in a semicolon, so eachimplmust write it.summaryhas a body, which callsself.title(), so it is inherited. - Predict before checking: Implement only
titlefor aPostwhose title isCrabs, and predict whatpost.summary()returns. Then predict what an emptyimpl Summary for Post {}does. - Compare with evidence:
summary()returnsTitle: Crabs. The emptyimplgets E0046 for the missingtitle, with no complaint aboutsummary.
Transfer challenge: Add a second struct that overrides summary and call both through fn show(item: &impl Summary). Then add a method with no body to the trait and count the impl blocks the compiler makes you update.
Connections
- Blanket Implementations: Implement behavior for every matching type
- Generic Lifetimes: Combine type bounds with borrow relationships
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Default Trait Methods (courseId "ownership", lessonId "default-trait-methods") Link: https://crabrix.com/learn/ownership/default-trait-methods/ Goal: A trait may provide a method body that implementations inherit. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/default-trait-methods) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.