特質
2022/2/20 6:52:00
pub fn notify(item: &(impl Summary + Display)) {
pub fn notify<T: Summary + Display>(item: &T) {
pub fn notify<T>(item: &T) where T:Summary + Display {
→pub fn notify<T>(item: &T) where T:Summary + Display {
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#複数のトレイト境界を構文で指定する
error[E0782]: trait objects must include the `dyn` keyword
--> src\main.rs:26:42
|
26 | fn run(config: Config) -> Result<(), Box<Error>> {
| ^^^^^
|
help: add `dyn` keyword before this trait
|
26 | fn run(config: Config) -> Result<(), Box<dyn Error>> {
| +++
pub fn notify(item: &impl Summary) {
println!("Breaking news! {}", item.summarize());
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#引数としてのトレイト
fn returns_summarizable() -> impl Summary {
Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
}
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#トレイトを実装している型を返す