t_wの輪郭

Feedlyでフォローするボタン

あれ

2022/2/26 8:54:00
fn test<T>(closure:T) where T: Fn(u32) -> u32 {
    assert_eq!(closure(10), 11);
}

let closure = |x| {
    x+1
};

test(closure);
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);
}