Bird
Raised Fist0
Rustprogramming~20 mins

Implementing traits in Rust - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of implementing a trait in Rust?
easy
A. To create a new data type
B. To define shared behavior that multiple types can use
C. To allocate memory for variables
D. To write unsafe code blocks

Solution

  1. Step 1: Understand what a trait represents

    A trait in Rust defines a set of methods that types can implement to share behavior.
  2. Step 2: Identify the purpose of implementing traits

    Implementing a trait allows different types to have common behavior, enabling polymorphism.
  3. Final Answer:

    To define shared behavior that multiple types can use -> Option B
  4. Quick Check:

    Traits = shared behavior [OK]
Hint: Traits define common behavior for many types [OK]
Common Mistakes:
  • Confusing traits with data types
  • Thinking traits allocate memory
  • Assuming traits are for unsafe code only
2. Which of the following is the correct syntax to implement a trait named Speak for a struct Dog?
easy
A. trait Speak for Dog { fn speak(&self); }
B. impl Dog for Speak { fn speak(&self) { println!("Woof!"); } }
C. impl Speak for Dog { fn speak(&self) { println!("Woof!"); } }
D. struct Dog impl Speak { fn speak(&self) { println!("Woof!"); } }

Solution

  1. Step 1: Recall the syntax for implementing a trait

    The correct syntax is impl TraitName for TypeName { ... }.
  2. Step 2: Match the syntax with the options

    impl Speak for Dog { fn speak(&self) { println!("Woof!"); } } correctly uses impl Speak for Dog and defines the method.
  3. Final Answer:

    impl Speak for Dog { fn speak(&self) { println!("Woof!"); } } -> Option C
  4. Quick Check:

    impl Trait for Type = correct syntax [OK]
Hint: Use 'impl Trait for Type' to implement traits [OK]
Common Mistakes:
  • Swapping trait and type names
  • Using trait keyword instead of impl
  • Placing impl inside struct
3. What will be the output of this Rust code?
trait Greet {
    fn greet(&self) -> String;
}

struct Person {
    name: String,
}

impl Greet for Person {
    fn greet(&self) -> String {
        format!("Hello, {}!", self.name)
    }
}

fn main() {
    let p = Person { name: String::from("Alice") };
    println!("{}", p.greet());
}
medium
A. Runtime panic
B. Hello, name!
C. Compilation error
D. Hello, Alice!

Solution

  1. Step 1: Understand trait implementation

    The trait Greet is implemented for Person with greet returning a formatted string with the person's name.
  2. Step 2: Trace the main function output

    Creating Person with name "Alice" and calling p.greet() returns "Hello, Alice!" which is printed.
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    Trait method returns greeting with name [OK]
Hint: Trait method returns formatted string with name [OK]
Common Mistakes:
  • Assuming trait methods print directly
  • Confusing variable names
  • Expecting compilation errors without reason
4. Identify the error in this trait implementation and select the fix:
trait Describe {
    fn describe(&self) -> String;
}

struct Book {
    title: String,
}

impl Describe for Book {
    fn describe(&self) {
        println!("{}", self.title);
    }
}
medium
A. Change fn describe(&self) to return String instead of ()
B. Add &mut self to the method signature
C. Remove the trait Describe
D. Change struct Book to enum Book

Solution

  1. Step 1: Check trait method signature

    The trait Describe requires describe to return a String.
  2. Step 2: Compare implementation signature

    The implementation returns nothing (()) because it only prints, causing a mismatch.
  3. Step 3: Fix the method to return a String

    Change the method to return self.title.clone() or a formatted string.
  4. Final Answer:

    Change fn describe(&self) to return String instead of () -> Option A
  5. Quick Check:

    Trait method return type must match [OK]
Hint: Match trait method return types exactly [OK]
Common Mistakes:
  • Ignoring return type mismatch
  • Adding unnecessary mutability
  • Changing struct type instead of method
5. You want to implement a trait Summary for two structs NewsArticle and Tweet. Both should have a summarize method returning a short string. How can you write a generic function notify that accepts any type implementing Summary and prints its summary?

Choose the correct function signature and body.
hard
A. fn notify(item: T) { println!("{}", item.summarize()); }
B. fn notify<T>(item: T) { println!("{}", item.summarize()); }
C. fn notify(item: Summary) { println!("{}", item.summarize()); }
D. fn notify(item: impl Summary) { println!("{}", item.summarize()); }

Solution

  1. Step 1: Understand trait bounds in generics

    To accept any type implementing Summary, use a generic type with a trait bound: T: Summary.
  2. Step 2: Analyze options for correct syntax

    fn notify(item: T) { println!("{}", item.summarize()); } uses generic syntax with trait bound correctly. fn notify(item: impl Summary) { println!("{}", item.summarize()); } uses impl Summary which is valid but less flexible for complex cases. fn notify<T>(item: T) { println!("{}", item.summarize()); } lacks trait bound, causing error. fn notify(item: Summary) { println!("{}", item.summarize()); } tries to use trait as a type, which is invalid.
  3. Final Answer:

    fn notify(item: T) { println!("{}", item.summarize()); } -> Option A
  4. Quick Check:

    Generic with trait bound = correct [OK]
Hint: Use generic with trait bound: fn f(x: T) [OK]
Common Mistakes:
  • Using trait as a type directly
  • Omitting trait bounds in generics
  • Confusing impl Trait and generic syntax
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