Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Created January 23, 2021 05:01
Show Gist options
  • Save harpiechoise/088dca49a8e2ad7e83e582ea293b007c to your computer and use it in GitHub Desktop.
Save harpiechoise/088dca49a8e2ad7e83e582ea293b007c to your computer and use it in GitHub Desktop.
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