【发布时间】:2014-10-30 07:26:06
【问题描述】:
考虑以下 sn-p:
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()。
我肯定在这里遗漏了一些明显的东西。按值从原始类型引用复制的惯用方法是什么?
【问题讨论】:
标签: rust