Complete the code to define a trait named Printable.
trait Printable {
fn [1](&self);
}The trait method is commonly named print to indicate printing behavior.
Complete the code to implement the Printable trait for struct Book.
struct Book {
title: String,
}
impl Printable for Book {
fn [1](&self) {
println!("Book: {}", self.title);
}
}The method name must match the trait definition, which is print.
Fix the error in the code by completing the trait bound syntax.
fn print_item<T: [1]>(item: T) { item.print(); }
The function requires the generic type T to implement the Printable trait to call print().
Fill both blanks to create a trait and implement it for a struct.
trait [1] { fn print(&self); } struct Article { content: String, } impl [2] for Article { fn print(&self) { println!("Article: {}", self.content); } }
The trait name and implementation must match exactly, here both are Printable.
Fill all three blanks to define a trait, implement it, and use it in a function.
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(); }
All trait references must be consistent and use Printable.
