Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a trait named Printable.
Rust
trait Printable {
fn [1](&self);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that are not conventional for printing.
✗ Incorrect
The trait method is commonly named print to indicate printing behavior.
2fill in blank
mediumComplete the code to implement the Printable trait for struct Book.
Rust
struct Book {
title: String,
}
impl Printable for Book {
fn [1](&self) {
println!("Book: {}", self.title);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the trait defines.
✗ Incorrect
The method name must match the trait definition, which is print.
3fill in blank
hardFix the error in the code by completing the trait bound syntax.
Rust
fn print_item<T: [1]>(item: T) { item.print(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated traits like Display or Debug.
✗ Incorrect
The function requires the generic type T to implement the Printable trait to call print().
4fill in blank
hardFill both blanks to create a trait and implement it for a struct.
Rust
trait [1] { fn print(&self); } struct Article { content: String, } impl [2] for Article { fn print(&self) { println!("Article: {}", self.content); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for trait and impl.
✗ Incorrect
The trait name and implementation must match exactly, here both are Printable.
5fill in blank
hardFill all three blanks to define a trait, implement it, and use it in a function.
Rust
trait [1] { fn print(&self); } struct Note { text: String, } impl [2] for Note { fn print(&self) { println!("Note: {}", self.text); } } fn show<T: [3]>(item: T) { item.print(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing trait names or using unrelated traits.
✗ Incorrect
All trait references must be consistent and use Printable.