あれ
fn hello(text:&str) {
println!("Hello, {}!", &text);
}
let m = Box::new(String::from("Rust"));
hello(&m); //Hello, Rust!
あれ
let x=5;
let y=Box::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
//assert_eq!(5, y); //can't compare `{integer}` with `Box<{integer}>`
ボックスで再帰的な型を可能にする
rustc –explain E0782 日本語訳
特質オブジェクトは dyn キーワードを含んでいなければなりません。
誤ったコード例
trait Foo {}
fn test(arg: Box<Foo>) {}. // エラー!
特質オブジェクトは、実行するまでわからないが、ある特質に適合する型のメソッドを呼び出すための方法です。
特質オブジェクトは Box<dyn Foo> で形成されるべきですが、上のコードでは dyn が省略されています。
このため、arg が単にヒープに確保された Foo という型ではなく、特質オブジェクトであることがわかりにくくなっています。
この問題を解決するには、特質名の前に dyn を追加します。
trait Foo {}
fn test(arg: Box<dyn Foo>){}。// ok!
2021版以前は許可されていましたが、現在はエラーとなります。