Abstractions in Practice · 14 min · free lesson

Default Trait Methods

Share behavior while allowing an override.

Core idea

A trait may provide a method body that implementations inherit.

Diagram for Default Trait Methods: a Rust code example flows to the lesson rule and the key observation.
The example makes the contract visible in code.

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.

Practise this code in Playground ↗

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
  1. title
  2. summary
  3. 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.

  1. Sort the methods: title ends in a semicolon, so each impl must write it. summary has a body, which calls self.title(), so it is inherited.
  2. Predict before checking: Implement only title for a Post whose title is Crabs, and predict what post.summary() returns. Then predict what an empty impl Summary for Post {} does.
  3. Compare with evidence: summary() returns Title: Crabs. The empty impl gets E0046 for the missing title, with no complaint about summary.

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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.