我正在测试 Rust 中的自定义双向迭代器,但遇到了错误。
error[E0382]: borrow of moved value: `iter`
--> src/main.rs:40:13
|
37 | let mut iter = BiIterator::from(vec![1, 2, 3]);
| -------- move occurs because `iter` has type `BiIterator<i32>`, which does not implement the `Copy` trait
38 | for i in iter {
| ----
| |
| `iter` moved due to this implicit call to `.into_iter()`
| help: consider borrowing to avoid moving into the for loop: `&iter`
39 | if i == 3 {
40 | iter.position(0);
| ^^^^^^^^^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `iter`
for i in &iter
是无用的,因为它不能编译(因为&iter
不可迭代),并且无法在循环内部调用position()
。对于 Rust 经验丰富的人来说,正确的解决方案是显而易见的,但对于初学者来说却不是那么简单。 - user4815162342