fn main() {
let x = &vec![1,2,3,5,7,11,13,17,19,23,29,31,37,41,47];
let mut i = x.iter().map(|x|{2*x});
while let Some(value) = i.next() {
println!("{}", value);
}
}
为什么这样可以工作?
如果我将let mut i =
改为 let i =
,就会出现错误:
|
3 | let i = x.iter().map(|x|{2*x});
| - help: consider changing this to be mutable: `mut i`
4 |
5 | while let Some(value) = i.next() {
| ^^^^^^^^ cannot borrow as mutable
问题:
- 我真的需要borrow as mutable吗?(也许我错过了什么。)
- 为什么我需要borrow as mutable?
- 这里实际上是可变的是什么?是i(可迭代对象)还是value(它的值)?
for value in i
不需要i
是可变的,因为它被移入了循环中(playground)。 - Jmb