0
0
Rustprogramming~20 mins

Implementing traits in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Trait Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of trait method call with default implementation

What is the output of this Rust code?

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

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 trait!
DNo output
Attempts:
2 left
💡 Hint

Check if the trait method has a default implementation.

Predict Output
intermediate
2:00remaining
Output when trait method is overridden

What will this Rust program print?

Rust
trait Speak {
    fn speak(&self) {
        println!("Speaking from trait");
    }
}

struct Dog;

impl Speak for Dog {
    fn speak(&self) {
        println!("Woof!");
    }
}

fn main() {
    let d = Dog;
    d.speak();
}
AWoof!
BRuntime error: method not found
CCompilation error: conflicting implementations
DSpeaking from trait
Attempts:
2 left
💡 Hint

Look at whether the trait method is overridden in the implementation.

🔧 Debug
advanced
2:30remaining
Why does this trait implementation cause a compilation error?

Identify the cause of the compilation error in this Rust code.

Rust
trait Calculate {
    fn calculate(&self, x: i32) -> i32;
}

struct Calculator;

impl Calculate for Calculator {
    fn calculate(&self, x: i32) -> i32 {
        42
    }
}

fn main() {
    let c = Calculator;
    println!("{}", c.calculate(10));
}
AMethod signature in impl does not match trait definition
BMissing semicolon after method body
CTrait method cannot return a value
DStruct Calculator does not implement the trait
Attempts:
2 left
💡 Hint

Compare the method signatures in the trait and the implementation.

📝 Syntax
advanced
2:30remaining
Which option correctly implements a trait for a generic struct?

Given the trait Displayable and a generic struct Wrapper<T>, which implementation is syntactically correct?

Rust
trait Displayable {
    fn display(&self) -> String;
}

struct Wrapper<T> {
    value: T,
}
A
impl Displayable for Wrapper&lt;T&gt; {
    fn display(&amp;self) -&gt; String {
        format!("Value: {:?}", self.value)
    }
}
B
impl&lt;T&gt; Displayable for Wrapper&lt;T&gt; {
    fn display(&amp;self) {
        println!("Value: {:?}", self.value);
    }
}
C
impl&lt;T&gt; Displayable for Wrapper {
    fn display(&amp;self) -&gt; String {
        format!("Value: {:?}", self.value)
    }
}
D
impl&lt;T&gt; Displayable for Wrapper&lt;T&gt; {
    fn display(&amp;self) -&gt; String {
        format!("Value: {:?}", self.value)
    }
}
Attempts:
2 left
💡 Hint

Remember to include generic parameters in both impl and trait implementation.

🚀 Application
expert
3:00remaining
What is the output of this trait object usage?

Consider this Rust code using trait objects. What will it print?

Rust
trait Animal {
    fn sound(&self) -> &'static str;
}

struct Cat;
struct Dog;

impl Animal for Cat {
    fn sound(&self) -> &'static str {
        "Meow"
    }
}

impl Animal for Dog {
    fn sound(&self) -> &'static str {
        "Woof"
    }
}

fn make_sound(animal: &dyn Animal) {
    println!("{}", animal.sound());
}

fn main() {
    let cat = Cat;
    let dog = Dog;
    let animals: Vec<&dyn Animal> = vec![&cat, &dog];
    for a in animals {
        make_sound(a);
    }
}
AWoof\nMeow
BMeow\nWoof
CCompilation error: cannot create trait object
DRuntime error: method not found
Attempts:
2 left
💡 Hint

Look at the order of elements in the vector and how trait objects work.