I have the following piece of code:
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;
}
}
I use a while
loop here since I sometimes need to skip a char in chars
.
However, this gives me the following error:
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
You can use the
strcursor
crate for this:It's better to iterate over something instead of using an index:
We
fuse
the iterator to avoid any issues with callingnext
after the firstNone
is returned.Your code has a number of issues:
Iterator::count
consumes the iterator. Once you've called that, the iterator is gone. That's the cause of your error. An alternate solution is to useIterator::by_ref
so that consuming the iterator you count isn't the end of the line.chars
is of typeChars
, which does not support indexing.chars[lex_index]
is nonsensical.You cannot compare a
char
to a string, sochars[lex_index] == "something"
wouldn't compile either. It's possible you could useChars::as_str
, but then you'd have to give upFuse
and deal with that yourself.