我有下面的代码:如何迭代字符列表,同时仍能够在迭代中跳过?
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
循环在这里,因为我有时需要跳过一个char在chars
。 然而,这给了我以下错误:
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