中級 › E0277: doesn't implement `std::fmt::Display` の原因と直し方 › パターン2
E0277: doesn't implement `std::fmt::Display` の原因と直し方
自作の構造体を{}(Display)で出力しようとすると、Displayトレイトを実装していない限りE0277エラーになります。
1 #[derive(Debug)] 2 struct Item { name: String, price: i32 } 3 fn main() { 4 let it = Item { name: String::from("pen"), price: 100 }; 5 println!("_____", it); ^ 6 }
error[E0277]: `Item` doesn't implement `std::fmt::Display`
空欄を埋めて実行を通す
自作の構造体はDisplayを自分で実装しない限り{}では表示できません。デバッグ目的なら{:?}を使います。
次の問題