0
0
Rustprogramming~20 mins

Defining traits in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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 program?

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

struct Person;

impl Greet for Person {}

fn main() {
    let p = Person;
    p.greet();
}
AHello from the trait!
BError: method greet not implemented
CHello from the struct!
DCompilation error due to missing impl
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!
BSpeaking from trait
CCompilation error: method speak missing
DRuntime error
Attempts:
2 left
💡 Hint

Check if the trait method is overridden in the implementation.

🔧 Debug
advanced
2:00remaining
Identify the error in trait implementation

What error does this Rust code produce?

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 calc = Calculator;
    println!("{}", calc.calculate(10));
}
ARuntime panic due to missing return
BError: missing semicolon in impl
CCompiles and prints 42
DError: method `calculate` has wrong signature in impl
Attempts:
2 left
💡 Hint

Compare the trait method signature with the implementation.

Predict Output
advanced
2:00remaining
Trait object method call behavior

Given this Rust code, what is the output?

Rust
trait Animal {
    fn sound(&self);
}

struct Cat;

impl Animal for Cat {
    fn sound(&self) {
        println!("Meow");
    }
}

fn make_sound(animal: &dyn Animal) {
    animal.sound();
}

fn main() {
    let c = Cat;
    make_sound(&c);
}
ARuntime error: trait object not implemented
BCompilation error: cannot use trait object
CNo output
DMeow
Attempts:
2 left
💡 Hint

Consider how trait objects work with dynamic dispatch.

📝 Syntax
expert
2:00remaining
Which option correctly defines a trait with an associated type?

Which of the following Rust trait definitions is syntactically correct?

A
trait Iterator {
    fn next(&mut self) -> Option<Self::Item>;
    type Item = i32;
}
B
trait Iterator {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;
}
C
trait Iterator {
    fn next(&mut self) -> Option<Self::Item>;
    type Item;
}
D
trait Iterator {
    type Item = i32;
    fn next(&mut self) -> Option<Item>;
}
Attempts:
2 left
💡 Hint

Check the correct order and syntax for associated types in traits.