Bird
Raised Fist0
Rustprogramming~20 mins

Default method implementations in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Default Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of default method call in trait

What is the output of this Rust code?

Rust
trait Greet {
    fn greet(&self) {
        println!("Hello from default method!");
    }
}

struct Person;

impl Greet for Person {}

fn main() {
    let p = Person;
    p.greet();
}
ARuntime error: method not found
BCompilation error: missing method implementation
CHello from default method!
DNo output
Attempts:
2 left
💡 Hint

Check if the trait method has a default implementation and if the struct implements the trait without overriding the method.

❓ Predict Output
intermediate
2:00remaining
Overriding default method in trait implementation

What will this Rust program print?

Rust
trait Animal {
    fn sound(&self) {
        println!("Some generic sound");
    }
}

struct Dog;

impl Animal for Dog {
    fn sound(&self) {
        println!("Bark");
    }
}

fn main() {
    let d = Dog;
    d.sound();
}
ABark
BSome generic sound
CCompilation error: method already defined
DRuntime error: method not found
Attempts:
2 left
💡 Hint

Look if the trait method is overridden in the implementation.

🔧 Debug
advanced
2:30remaining
Why does this code fail to compile?

Consider this Rust code. Why does it fail to compile?

Rust
trait Calculator {
    fn calculate(&self) -> i32;
    fn double(&self) -> i32 {
        self.calculate() * 2
    }
}

struct MyCalc;

impl Calculator for MyCalc {}

fn main() {
    let c = MyCalc;
    println!("{}", c.double());
}
AError because default method <code>double</code> cannot call <code>calculate</code>
BError because <code>calculate</code> is not implemented in <code>MyCalc</code>
CError because <code>double</code> must be marked as <code>default</code>
DNo error, prints 0
Attempts:
2 left
💡 Hint

Check if all required trait methods are implemented.

🧠 Conceptual
advanced
1:30remaining
Purpose of default method implementations in traits

What is the main purpose of providing default method implementations in Rust traits?

ATo force all implementors to override the method
BTo make the trait methods private
CTo prevent the trait from being implemented by structs
DTo allow trait methods to have a common behavior that can be used or overridden by implementors
Attempts:
2 left
💡 Hint

Think about code reuse and flexibility in trait design.

❓ Predict Output
expert
3:00remaining
Output of trait object calling default and overridden methods

What is the output of this Rust program?

Rust
trait Speaker {
    fn speak(&self) {
        println!("Default speak");
    }
}

struct Cat;

impl Speaker for Cat {
    fn speak(&self) {
        println!("Meow");
    }
}

struct Robot;

impl Speaker for Robot {}

fn main() {
    let animals: Vec<&dyn Speaker> = vec![&Cat, &Robot];
    for animal in animals {
        animal.speak();
    }
}
AMeow\nDefault speak
BDefault speak\nMeow
CMeow\nMeow
DDefault speak\nDefault speak
Attempts:
2 left
💡 Hint

Check which structs override the speak method and which use the default.

Practice

(1/5)
1. What is the main purpose of default method implementations in Rust traits?
easy
A. To automatically generate structs from traits
B. To force all types to implement the method themselves
C. To prevent any method from being used outside the trait
D. To provide reusable behavior that types can use or override

Solution

  1. Step 1: Understand trait default methods

    Default methods in traits provide a common implementation that types can use without writing their own code.
  2. Step 2: Compare with other options

    Options A, B, and C describe behaviors that are not true for default methods in Rust traits.
  3. Final Answer:

    To provide reusable behavior that types can use or override -> Option D
  4. Quick Check:

    Default methods = reusable behavior [OK]
Hint: Default methods give ready code to reuse or replace [OK]
Common Mistakes:
  • Thinking default methods force implementation
  • Confusing traits with structs
  • Believing default methods hide methods
2. Which of the following is the correct syntax to define a default method in a Rust trait?
easy
A. fn greet() { println!("Hello"); }
B. fn greet();
C. fn greet() -> { println!("Hello"); }
D. fn greet() => println!("Hello");

Solution

  1. Step 1: Recall Rust trait method syntax

    Default methods in traits have a body with curly braces, like fn greet() { ... }.
  2. Step 2: Check each option

    fn greet() { println!("Hello"); } uses correct syntax with braces. fn greet(); is a method signature without body (no default). Options A and D have invalid syntax.
  3. Final Answer:

    fn greet() { println!("Hello"); } -> Option A
  4. Quick Check:

    Default method = fn name() { body } [OK]
Hint: Default methods always have braces with code inside [OK]
Common Mistakes:
  • Omitting braces for default methods
  • Using arrow syntax instead of braces
  • Writing only method signatures without body
3. What will be the output of this Rust code?
trait Speak {
    fn say(&self) {
        println!("Default speaking");
    }
}

struct Dog;

impl Speak for Dog {}

fn main() {
    let d = Dog;
    d.say();
}
medium
A. Dog says woof
B. Compilation error: method say not implemented
C. Default speaking
D. No output

Solution

  1. Step 1: Analyze trait and struct implementation

    The trait Speak has a default method say that prints "Default speaking". Dog implements Speak but does not override say.
  2. Step 2: Determine method called

    Since Dog does not override say, calling d.say() uses the default method from the trait.
  3. Final Answer:

    Default speaking -> Option C
  4. Quick Check:

    Default method runs if not overridden [OK]
Hint: If no override, default method runs [OK]
Common Mistakes:
  • Expecting an error for missing method
  • Assuming default method is not used
  • Confusing struct behavior with trait methods
4. Identify the error in this Rust code using default method implementations:
trait Talk {
    fn talk(&self) {
        println!("Talking");
    }
}

struct Cat;

impl Talk for Cat {
    fn talk(&self);
}

fn main() {
    let c = Cat;
    c.talk();
}
medium
A. Trait Talk must not have default methods
B. Missing method body in impl causes compilation error
C. Struct Cat cannot implement Talk
D. No error, code runs fine

Solution

  1. Step 1: Check impl block for talk method

    The impl for Cat declares fn talk(&self); without a body, which is invalid syntax in Rust.
  2. Step 2: Understand default method usage

    Since Talk trait has a default talk method, Cat can omit implementing it or provide a full method body. Declaring without body causes error.
  3. Final Answer:

    Missing method body in impl causes compilation error -> Option B
  4. Quick Check:

    Impl methods need bodies unless default used [OK]
Hint: Impl methods must have bodies or be omitted [OK]
Common Mistakes:
  • Declaring method without body in impl
  • Thinking default method forces impl
  • Ignoring compiler error messages
5. Given this trait and structs, how can you override the default method only for Cat while Dog uses the default?
trait Animal {
    fn sound(&self) {
        println!("Some sound");
    }
}

struct Dog;
struct Cat;

impl Animal for Dog {}
impl Animal for Cat {
    // What goes here?
}

fn main() {
    let dog = Dog;
    dog.sound();
    let cat = Cat;
    cat.sound();
}
hard
A. fn sound(&self) { println!("Meow"); }
B. fn sound(&self);
C. fn sound() { println!("Meow"); }
D. fn sound(&self) -> String { "Meow".to_string() }

Solution

  1. Step 1: Understand overriding default methods

    To override a default method, provide a method with the same signature and a body in the impl block.
  2. Step 2: Check options for correct override syntax

    fn sound(&self) { println!("Meow"); } correctly defines fn sound(&self) with a body printing "Meow". fn sound(&self); lacks body, causing error. fn sound() { println!("Meow"); } misses &self parameter. fn sound(&self) -> String { "Meow".to_string() } changes return type, incompatible with trait.
  3. Final Answer:

    fn sound(&self) { println!("Meow"); } -> Option A
  4. Quick Check:

    Override = same signature + method body [OK]
Hint: Override with same signature and method body [OK]
Common Mistakes:
  • Omitting &self parameter
  • Declaring method without body
  • Changing return type in override
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard