は、私は次のコードを持っている:私は時々chars
で文字をスキップする必要があるため反復でスキップできるうちに、どのように文字リストを反復処理するのですか?
let mut lex_index = 0;
let chars = expression.chars();
while lex_index < chars.count() {
if(chars[lex_index] == "something") {
lex_index += 2;
} else {
lex_index += 1;
}
}
は、私がここにwhile
ループを使用します。最初の後next
を呼んですべての問題を回避するために
let mut chars = "gravy train".chars().fuse();
while let Some(c) = chars.next() {
if c == 'x' {
chars.next(); // Skip the next one
}
}
我々はイテレータをfuse
:
error[E0382]: use of moved value: `chars`
--> src/main.rs:23:15
|
23 | while i < chars.count() {
| ^^^^^ value moved here in previous iteration of loop
|
= note: move occurs because `chars` has type `std::str::Chars<'_>`, which does not implement the `Copy` trait
'の文字を<>'イテレータではなく、コレクションですので、あなたはそのようにとにかくそれへのインデックスすることはできません。 – ildjarn
文字列をスキップしたいときに 'continue'を使用してください。 – Boiethios
これは本当に気づいていますが、これは私が探している動作の種類を説明するために思いついたコードです。 – duck