In Rust, traits define shared behavior that types can implement. A trait acts as a contract, requiring implementing types to provide certain methods. This makes traits one of the most powerful features for achieving polymorphism and code reusability in Rust.
For example, the Display
trait allows custom formatting for types using the {}
formatter. Similarly, you can define your own traits to describe behaviors relevant to your program.
In this challenge, you'll implement a trait Describable
and use it to define a common interface for objects that can provide a description. Your task is to:
Describable
with a single method describe
that returns a String
.Person
.Book
.Person
struct should have fields name: String
and age: u8
.Book
struct should have fields title: String
and author: String
.describe
method for Person
should return a string like "Person: Alice, Age: 30"
.describe
method for Book
should return a string like "Book: Rust Programming, Author: Jane Doe"
.If you're stuck, here are some hints to help you solve the challenge:
impl TraitName for StructName
. e.g.
impl Describable for Person {
fn describe(&self) -> String {
format!("Person: {}, Age: {}", self.name, self.age)
}
}
&self
as the parameter for the describe
method in the trait.pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}pub struct Book { pub title: String, pub author: String,}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}#[derive(Debug)]pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self )-> String{ format!("Person: {}, Age: {}" , self.name , self.age) }}impl Describable for Book { fn describe(&self )-> String{ format!("Book: {}, Author: {}", self.title , self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// // TODO: Implement the `Describable` trait for `Person` and `Book`Define a trait Describable with a single method describe that returns a String.// Implement this trait for the struct Person.// Implement this trait for the struct Book.// Requirements// The Person struct should have fields name: String and age: u8.// The Book struct should have fields title: String and author: String.// The describe method for Person should return a string like "Person: Alice, Age: 30".// The describe method for Book should return a string like "Book: Rust Programming, Author: Jane Doe".// Implement the `Describable` trait for `Person`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}// Implement the `Describable` trait for `Book`impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {0}, Age: {1}", self.name, self.age) }}pub struct Book { pub title: String, pub author: String,}impl Describable for Book { fn describe(&self) -> String { format!("Book: {0}, Author: {1}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {0}, Age: {1}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {0}, Author: {1}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { let name = &self.name; let age = &self.age; format!("Person: {}, Age: {}", name, age) }}impl Describable for Book { fn describe(&self) -> String { let title = &self.title; let author = &self.author; format!("Book: {}, Author: {}", title, author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person{ fn describe(&self) -> String { String::from(format!("Person: {}, Age: {}",self.name, self.age)) }}impl Describable for Book { fn describe(&self) -> String { String::from(format!("Book: {}, Author: {}",self.title, self.author)) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe (&self) -> String { format!("Person: {}, Age: {}", self.name.to_string(), self.age.to_string()) }}impl Describable for Book { fn describe (&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { let name = &self.name; let age = &self.age; format!("Person: {name}, Age: {age}") }}impl Describable for Book { fn describe(&self) -> String { let title = &self.title; let author = &self.author; format!("Book: {title}, Author: {author}") }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}pub struct Book { pub title: String, pub author: String,}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person{ fn describe(&self) -> String{ format!("Person: {}, Age: {}",self.name,self.age) }}impl Describable for Book{ fn describe(&self) -> String{ format!("Book: {}, Author: {}",self.title,self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {0}, Age: {1}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {0}, Author: {1}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age ) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author ) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}",self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}",self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}impl Person { pub fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}pub struct Book { pub title: String, pub author: String,}impl Book { pub fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age).to_string() }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author).to_string() }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", &self.name, &self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", &self.title, &self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { return format!("Person: {}, Age: {}", self.name, self.age); }}impl Describable for Book { fn describe(&self) -> String { return format!("Book: {}, Author: {}", self.title, self.author); }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String{ format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String{ format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}pub struct Book { pub title: String, pub author: String,}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// TODO: Implement the `Describable` trait for `Person` and `Book`// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Person{ fn describe(&self) -> String{ format!("Person: {}, Age: {}",self.name, self.age) }}impl Describable for Book{ fn describe(&self) -> String{ format!("Book: {}, Author: {}",self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}
pub trait Describable { fn describe(&self) -> String;}pub struct Person { pub name: String, pub age: u8,}pub struct Book { pub title: String, pub author: String,}// TODO: Implement the `Describable` trait for `Person` and `Book`impl Describable for Book { fn describe(&self) -> String { format!("Book: {}, Author: {}", self.title, self.author) }}impl Describable for Person { fn describe(&self) -> String { format!("Person: {}, Age: {}", self.name, self.age) }}// Example usagepub fn main() { let person = Person { name: "Alice".to_string(), age: 30, }; let book = Book { title: "Rust Programming".to_string(), author: "Jane Doe".to_string(), }; println!("{}", person.describe()); println!("{}", book.describe());}