考虑下面的片段:
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);
}
}
编译器抱怨i
是&mut usize
而不是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.clone()
.
我肯定错过了一些显而易见的东西.按值从基元类型引用复制的惯用方法是什么?