iter()
on Vec<T>
returns an iterator implementing Iterator<&T>
, that is, this iterator will yield references into the vector. This is the most general behavior which allows convenient usage with non-copyable types.
However, primitive types (actually, any types which implement Copy
trait) will be copied upon dereference anyway, so you just need this:
for i in current_items.iter() {
let mut result = all_items.get_mut(*i);
}
Alternatively, you can use reference destructuring pattern:
for &i in current_items.iter() {
let mut result = all_items.get_mut(i);
}
Now i
is usize
automatically and you don't need to dereference it manually.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…