特質
2022/2/20 6:52:00
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 + 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#複数のトレイト境界を構文で指定する
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#トレイトを実装している型を返す
pub fn notify(item: &impl Summary) {
println!("Breaking news! {}", item.summarize());
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#引数としてのトレイト
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#デフォルト実装
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#トレイトを型に実装する
pub trait Summary {
fn summarize(&self) -> String;
}
https://doc.rust-jp.rs/book-ja/ch10-02-traits.html#トレイトを定義する
Copy
特質を持たない変数をコピーすると、所有権の移動が発生し、コピー元の変数は使用が不可能になる。
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1); //error
/*
error[E0382]: borrow of moved value: `s1`
--> src\main.rs:5:16
|
3 | let s1 = String::from("hello");
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
4 | let s2 = s1;
| -- value moved here
5 | println!("{}", s1); //hello
| ^^ value borrowed here after move
*/
Copy
特質を持たない変数を関数に渡すと、所有権の移動が発生し、関数に渡した変数は使用不可能になる。
fn main() {
let s = String::from("hello");
test(s);
println!("{}", s); //error
/*
error[E0382]: borrow of moved value: `s`
--> src\main.rs:4:20
|
2 | let s = String::from("hello");
| - move occurs because `s` has type `String`, which does not implement the `Copy` trait
3 | test(s);
| - value moved here
4 | println!("{}", s); //error
| ^ value borrowed here after move
*/
}
fn test(s:String) {
println!("{}", s); //hello
}