上級 › E0782: trait objects must include the `dyn` keyword の原因と直し方 › パターン5
E0782: trait objects must include the `dyn` keyword の原因と直し方
トレイトを直接型として使おうとすると、コンパイル時のサイズが確定できないためエラーになります。
1 trait Payment { 2 fn amount(&self) -> i32; 3 } 4 struct Cash; 5 impl Payment for Cash { 6 fn amount(&self) -> i32 { 500 } 7 } 8 fn main() { 9 let payments: Vec<Box<___________>> = vec![Box::new(Cash)]; ^ 10 println!("{}", payments[0].amount()); 11 }
error[E0782]: trait objects must include the `dyn` keyword
空欄を埋めて実行を通す
トレイト名だけを型のように書くのはRustの初期バージョンの名残で、現在は非推奨としてエラーになります。
次の問題