Rust エラー「error[E0277]: Vec<&str> doesn’t implement std::fmt::Display」が発生した場合の対処法
![Rust エラー「error[E0277]: Vec<&str> doesn’t implement std::fmt::Display」が発生した場合の対処法](https://mebee.info/wp-content/uploads/2019/11/rust.png)
Rustで、エラー「error[E0277]: Vec<&str> doesn’t implement std::fmt::Display」が発生した場合の対処法を記述してます。「println!」でベクタを表示した際に発生。Rustのバージョンは1.65.0を使用してます。
環境
- OS windows11 home
- rustc 1.65.0
エラー全文
以下のコードで発生
fn main() {
let str: String = "aaa-bbb-ccc".to_string();
println!("{}", str.split(|e| e == '-').collect::<Vec<_>>());
}
エラー全文
error[E0277]: `Vec<&str>` doesn't implement `std::fmt::Display`
--> src\main.rs:5:20
|
5 | println!("{}", str.split(|e| e == '-').collect::<Vec<_>>()); // ["aaa", "bbb", "ccc"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Vec<&str>` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Vec<&str>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `rust_sample` due to previous error
原因
「 {} 」の「fmt::Display」でベクタは表示できないため
対処法
「{:?} 」か「{:#?}」を使用する
fn main() {
let str: String = "aaa-bbb-ccc".to_string();
println!(":? = {:?}", str.split(|e| e == '-').collect::<Vec<_>>());
println!(":#? = {:#?}", str.split(|e| e == '-').collect::<Vec<_>>());
}
実行結果

-
前の記事
mongoDB 割り算の余りの結果同じデータを取得する 2022.11.15
-
次の記事
.net7.0のインストール手順 2022.11.15
コメントを書く