Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused_variables, dead_code)]
- pub trait Summary {
- fn summarize(&self) -> String;
- }
- pub struct Tweet {
- pub username: String,
- pub content: String,
- pub reply: bool,
- pub retweet: bool,
- }
- impl Summary for Tweet {
- fn summarize(&self) -> String {
- format!("{}: {}", self.username, self.content)
- }
- }
- // Use trait bounds to say what functionality the param must implement.
- fn notify<T: Summary>(item: T) {
- println!("Breaking news! {}", item.summarize());
- }
- fn main() {
- let tweet = Tweet {
- username: String::from("horse_ebooks"),
- content: String::from("of course, as you probably already know, people"),
- reply: false,
- retweet: false,
- };
- notify(tweet);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement