0
0
Rustprogramming~20 mins

Default method implementations in Rust - Practice Problems & Coding Challenges

Choose your learning style9 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.