前提・実現したいこと
rfind関数を作成しようとしました。
発生している問題・エラーメッセージ
error[E0277]: the trait bound `Chars<'_>: ExactSizeIterator` is not satisfied --> Main.rs:5:48 | 5 | for (idex, val) in arg.chars().enumerate().rev() { | ^^^ the trait `ExactSizeIterator` is not implemented for `Chars<'_>` | = note: required because of the requirements on the impl of `DoubleEndedIterator` for `Enumerate<Chars<'_>>` error[E0277]: the trait bound `Chars<'_>: ExactSizeIterator` is not satisfied --> Main.rs:5:24 | 5 | for (idex, val) in arg.chars().enumerate().rev() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ExactSizeIterator` is not implemented for `Chars<'_>` | = note: required because of the requirements on the impl of `DoubleEndedIterator` for `Enumerate<Chars<'_>>` = note: required because of the requirements on the impl of `Iterator` for `Rev<Enumerate<Chars<'_>>>` = note: required because of the requirements on the impl of `IntoIterator` for `Rev<Enumerate<Chars<'_>>>` = note: required by `into_iter` error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`.
該当のソースコード
Rust
1fn rfind(arg: &str, moji: char) -> isize { 2 use std::convert::TryInto; 3 for (idex, val) in arg.chars().enumerate().rev() { 4 if val == moji { 5 return idex.try_into().unwrap(); 6 } 7 } 8 return -1; 9} 10fn main() { 11 println!("{}", rfind("abcdcba", 'a')); 12}
試したこと
Rust Iterator revにはDoubleEndedIterators
でのみ動作するとあったので、終端がわかるように何かしらのcollectionに変換すればよいと思い、collectを使ってVec, VecDeque, LinkedListに変換してみましたが、効果はありませんでした。
どのようにすれば思い通りになるのでしょうか、回答よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
cargo 1.58.0 (f01b232bc 2022-01-19)
rustc 1.58.1 (db9d1b20b 2022-01-20)
rustup 1.23.1 (3df2264a9 2020-11-30)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。