【发布时间】:2021-11-13 10:08:11
【问题描述】:
我正在尝试为一个简单的特征实现 Index 特征,并且我想将它与 usize 一起使用。我添加了SliceIndex<[T], Output = T>,所以我可以使用T 来索引slice 内的A。
use std::ops::Index;
use std::slice::SliceIndex;
struct A <'a, T>{
slice: &'a [T]
}
impl<'a, T: Index<T, Output = T> + SliceIndex<[T], Output = T>> Index<T>
for A<'a, T>
{
type Output = T;
#[inline(always)]
fn index(&self, index: T) -> &Self::Output {
self.slice.index(index)
}
}
fn main() {
let mut aa: Vec<u64> = vec![0; 10];
let coefficient_iterable = A{slice: &aa};
println!("{}", coefficient_iterable[1usize]);
}
但我明白了:
错误:
error[E0608]: cannot index into a value of type `A<'_, u64>`
--> src/main.rs:22:20
|
22 | println!("{}", coefficient_iterable[1usize]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0608`.
error: could not compile `playground` due to previous error
我不知道为什么,因为usize 实现了SliceIndex<[T]>。
【问题讨论】:
标签: rust