t_wの輪郭

Rust

impl

2022/2/17 16:16:00
あれ引数の型が複数の特質を実装していることを指定する戻り値の型が特質を実装していることを指定する引数の型として特質を使う特質のデフォルト実装

あれ

2022/2/25 23:51:00
fn test(closure: impl Fn(u32)->u32) {
    assert_eq!(closure(10), 11);
}

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

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

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

    println!("{}", news_article.summarize());
}

https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#デフォルト実装