use std::fmt::{Display, Debug};
pub struct NewsArticle{
pub author: String,
pub headline: String,
pub content: String
}
pub struct Tweet{
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool
}
pub trait Summary{
fn summarize(&self) -> String{
format!("Read more from {}...", self.summarize_author()) }
fn summarize_author(&self) -> String;
}
impl Summary for NewsArticle{
fn summarize_author(&self) -> String {
format!("@{}", self.author)
}
}
impl Summary for Tweet{
fn summarize(&self) -> String {
format!("@{}: {}", self.username, self.content)
}
fn summarize_author(&self) -> String {
format!("@{}", self.username)
}
}
pub fn notify<T: Summary>(item1: &T, item2: &T){ println!("Breaking news! \n{}\n{}", item1.summarize(), item2.summarize());
}
fn some_function<T, U>(t: &T, u: &U)
where T: Display + Clone,
U: Clone + Debug{
}
fn returns_summarizable() -> impl Summary{ Tweet{
username: String::from("Revv"),
content: String::from("of course, as you probably already know, people"),
reply: false,
retweet: false
}
}
struct Pair<T>{
x: T,
y: T
}
impl<T> Pair<T>{
fn new(x: T, y: T) -> Self{
Self{x, y}
}
}
impl<T: Display + PartialOrd> Pair<T>{
fn cmp_display(&self){
if self.x >= self.y{
println!("The largest member is x = {}", self.x);
}
else{
println!("The largest member is y = {}", self.y);
}
}
}
pub fn main(){
let tweet = Tweet{
username: String::from("Revv"),
content: String::from("of course, as you probably already know, people"),
reply: false,
retweet: false
};
let article = NewsArticle{
author: String::from("Revv"),
headline: String::from("This is a headline"),
content: String::from("This is the content")
};
println!("1 new tweet: {}", tweet.summarize());
println!("1 new article: {}", article.summarize());
notify(&article, &article);
println!("{:?}", returns_summarizable().summarize());
let var_int = Pair{x: 1, y: 2};
var_int.cmp_display(); }