Consider the following snippet:
fn example(current_items: Vec<usize>, mut all_items: Vec<i32>) {
for i in current_items.iter() {
let mut result = all_items.get_mut(i);
}
}
The compiler is complaining about i
being &mut usize
instead of usize
:
error[E0277]: the trait bound `&usize: std::slice::SliceIndex<[()]>` is not satisfied
--> src/lib.rs:3:36
|
3 | let mut result = all_items.get_mut(i);
| ^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[()]>` is not implemented for `&usize`
I've dug through the docs but the only way I see to satisfy the compiler is i.clone()
.
I'm definitely missing something obvious here. What's the idiomatic way to copy from primitive type reference by value?