【发布时间】:2017-07-15 20:25:53
【问题描述】:
我有以下代码:
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 循环,因为有时我需要跳过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
【问题讨论】:
-
Chars<>是一个迭代器,而不是一个集合,所以无论如何你都不能这样索引它。 -
当你想跳过一个字符时,只需使用
continue... -
我确实注意到了这一点,但这是我想出的一段快速代码来说明我正在寻找的行为类型。
标签: while-loop rust iteration chars