【发布时间】:2022-06-28 21:36:26
【问题描述】:
我正在尝试查看当前位置前面的字符,同时遍历 &str。
let myStr = "12345";
let mut iter = myStr.chars().peekable();
for c in iter {
let current: char = c;
let next: char = *iter.peek().unwrap_or(&'∅');
}
我将把这个字符传递给一个方法。然而,即使是这个 MRE 也会产生一个我不知道如何过去的移动错误之后的借用。
error[E0382]: borrow of moved value: `iter`
--> src/lib.rs:7:27
|
4 | let mut iter = myStr.chars().peekable();
| -------- move occurs because `iter` has type `Peekable<Chars<'_>>`, which does not implement the `Copy` trait
5 | for c in iter {
| ---- `iter` moved due to this implicit call to `.into_iter()`
6 | let current: char = c;
7 | let next: char = *iter.peek().unwrap_or(&'∅');
| ^^^^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `iter`
--> /home/james/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:267:18
|
267 | fn into_iter(self) -> Self::IntoIter;
知道这里发生了什么吗?我尝试了各种引用和取消引用的组合,但似乎没有任何效果。
【问题讨论】:
标签: rust borrow-checker