t_wの輪郭

Feedlyでフォローするボタン
use std::fmt;

trait Summary {
    fn summarize(&self) -> String {
        String::from("(Read more...)")
    }
}

struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {}
impl fmt::Display for NewsArticle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "({}, {}, {}, {})", self.headline, self.location, self.author, self.content)
    }
}

fn notify<T>(item:T) where T:Summary + fmt::Display {
    println!("Breaking news! {}", item.summarize());
}

fn main() {
    let news_article = NewsArticle{
        headline: String::from("Headline"), 
        location: String::from("somewhere"), 
        author: String::from("somebody"), 
        content: String::from("something")
    };

    notify(news_article);
}