窥视迭代器内部

发布于 2025-02-11 06:35:22 字数 1243 浏览 2 评论 0原文

我试图窥视我当前位置的char段,同时迭代& 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;

知道这里发生了什么事吗?我已经尝试了参考和删除的各种组合,但我尝试过任何尝试似乎没有用。

I'm trying to peek at the char in-front of my current location whilst iterating over a &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(&'∅');
}

I will be passing this char into a method down the line. However, even this MRE produces a borrow after move error that I'm not sure how to get past.

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;

Any idea what's going on here? I've tried various combinations of referencing and dereferencing but nothing I've tried seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的海 2025-02-18 06:35:22

迭代器移至循环的中。您无法在循环的内手动操纵迭代器。但是,循环的可以用替换

while let Some(c) = iter.next() {
    let current: char = c;
    let next: char = *iter.peek().unwrap_or(&'∅');
}

Playground

The iterator is moved into the for loop. You cannot manually manipulate an iterator inside a for loop. However, the for loop can be replaced by while let:

while let Some(c) = iter.next() {
    let current: char = c;
    let next: char = *iter.peek().unwrap_or(&'∅');
}

Playground.

不离久伴 2025-02-18 06:35:22

如果您可以使用切片,则使用windows()

let slice = ['1', '2', '3', '4', '5'];
let iter = slice.windows(2);
for arr in iter {
    let current = arr[0];
    let next = arr[1];
}

Playground

If you can work with slices, it will get much easier with windows():

let slice = ['1', '2', '3', '4', '5'];
let iter = slice.windows(2);
for arr in iter {
    let current = arr[0];
    let next = arr[1];
}

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文