0
0
Rustprogramming~10 mins

Default method implementations in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default method implementations
Define Trait with Default Method
Implement Trait for Struct
Use Default Method from Trait
Call Method on Struct Instance
Default Method Runs
Output Result
This flow shows how a trait with a default method is defined, implemented by a struct, and how calling the method uses the default implementation.
Execution Sample
Rust
trait Greet {
    fn say_hello(&self) {
        println!("Hello from default method!");
    }
}

struct Person;

impl Greet for Person {}

fn main() {
    let p = Person;
    p.say_hello();
}
This Rust code defines a trait with a default method, implements it for a struct without overriding, then calls the default method.
Execution Table
StepActionEvaluationResult
1Define trait Greet with default method say_helloTrait created with default methodTrait Greet with say_hello() exists
2Define struct PersonStruct createdStruct Person exists
3Implement trait Greet for Person without overriding say_helloImplementation uses default methodPerson implements Greet using default say_hello
4Create instance p of PersonInstance createdp is Person instance
5Call p.say_hello()No override, calls default methodPrints: Hello from default method!
6Program endsNo more codeExecution stops
💡 Program ends after calling default method once
Variable Tracker
VariableStartAfter Step 4Final
pundefinedPerson instancePerson instance
Key Moments - 2 Insights
Why does calling say_hello() on Person print the default message?
Because in step 3 (execution_table row 3), Person implements Greet but does not override say_hello, so the default method from the trait is used.
Can we override the default method in the implementation?
Yes, but in this example (step 3), we did not override, so the default runs. Overriding would replace the default method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when p.say_hello() is called at step 5?
AHello from default method!
BNo output
CCompilation error
DCustom message from Person
💡 Hint
See execution_table row 5 where the default method prints the message.
At which step is the Person instance created?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check execution_table row 4 where instance p is created.
If we override say_hello in Person's impl, what changes in the execution?
AThe default method is still called
BCompilation fails
CThe overridden method runs instead
DThe method call is ignored
💡 Hint
Refer to key_moments where overriding replaces the default method.
Concept Snapshot
trait TraitName {
    fn method(&self) { // default implementation
        // code
    }
}

struct StructName;

impl TraitName for StructName {
    // can override method or use default
}

// Calling method on StructName instance uses default if not overridden
Full Transcript
This example shows how Rust traits can have default method implementations. We define a trait Greet with a default method say_hello that prints a message. Then we create a struct Person and implement Greet for it without overriding say_hello. When we create an instance of Person and call say_hello, the default method runs and prints the message. This demonstrates how default methods provide reusable behavior that can be used or overridden by implementers.