Created
January 23, 2021 05:01
-
-
Save harpiechoise/088dca49a8e2ad7e83e582ea293b007c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait Summary { | |
fn sumarize(&self) -> String { | |
format!("(Read more from {}...)", self.sumarize_author()) | |
} | |
fn sumarize_author(&self) -> String; | |
} | |
pub trait User { | |
fn printUser(&self) -> (); | |
} | |
pub struct NewsArticle { | |
pub headline: String, | |
pub location: String, | |
pub author: String, | |
pub content: String, | |
} | |
pub struct Tweet { | |
pub username: String, | |
pub content: String, | |
pub reply: bool, | |
pub retweet: bool, | |
} | |
impl Summary for NewsArticle { | |
fn sumarize(&self) -> String { | |
format!("{}, by {} ({})", self.headline, self.sumarize_author(), self.location) | |
} | |
fn sumarize_author(&self) -> String { | |
format!("{}", self.author) | |
} | |
} | |
impl Summary for Tweet { | |
fn sumarize(&self) -> String { | |
format!("{}: {}", self.sumarize_author(), self.content) | |
} | |
fn sumarize_author(&self) -> String { | |
format!("@{}", self.username) | |
} | |
} | |
impl User for Tweet { | |
fn printUser(&self) { | |
println!("{}",self.username) | |
} | |
} | |
impl User for NewsArticle { | |
fn printUser(&self) { | |
println!("{}", self.author) | |
} | |
} | |
pub fn notifyUser<T, U> (item: &T, item2: &U) | |
where T: Summary + User, | |
U: Summary + User { | |
item.printUser(); | |
println!("Summary: {}", item.sumarize()); | |
item2.printUser(); | |
println!("Summary: {}", item.sumarize()); | |
} | |
fn main() { | |
let tweet = Tweet { | |
username: String::from("my_ebooks"), | |
content: String::from("I'm learning Rust"), | |
reply: false, | |
retweet: false, | |
}; | |
let article = NewsArticle { | |
headline: String::from("Penguins win the Stanley Cup Championship!"), | |
location: String::from("Pittsburgh, PA, USA"), | |
author: String::from("Iceburgh"), | |
content: String::from( | |
"The Pittsburgh Penguins once again are the best \ | |
hockey team in the NHL.", | |
), | |
}; | |
println!("1 New Tweet {}", tweet.sumarize()); | |
println!("New article avaible {}", article.sumarize()); | |
notify(&tweet); | |
notifyUser(&tweet, &article); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment