特質を定義する
2022/2/20 6:55:00
pub trait Summary {
fn summarize(&self) -> String;
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#トレイトを定義する
pub trait Summary {
fn summarize(&self) -> String;
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#トレイトを定義する
trait Summary {
fn summarize(&self) -> String;
}
struct NewsArticle {
pub headline: String,
pub location: String,
pub author: String,
pub content: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {} ({})", self.headline, self.author, self.location)
}
}
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#トレイトを型に実装する