我正在尝试从一个Read切片创建一个u8特性对象,用于murmur3机箱中,如下所示
fn main() {
let mut arr: [u8; 4] = [1, 2, 3, 4];
let mut slice: &mut [u8] = &mut arr;
let mut read: &mut std::io::Read = &mut slice;
}但我得到
<anon>:4:42: 4:53 error: the trait `std::io::Read` is not implemented for the type `[u8]` [E0277]
<anon>:4 let mut read : & mut std::io::Read = & mut slice;
^~~~~~~~~~~
<anon>:4:42: 4:53 help: see the detailed explanation for E0277
<anon>:4:42: 4:53 help: the following implementations were found:
<anon>:4:42: 4:53 help: <&'a [u8] as std::io::Read>
<anon>:4:42: 4:53 note: required for the cast to the object type `std::io::Read`
error: aborting due to previous error这个密码怎么了?
发布于 2016-06-17 12:20:35
正如错误消息所告诉您的,&[u8]有一个&[u8]驱动程序。没有理由对Read进行&mut[u8]驱动,所以您只需删除代码中的一些mut:
// no need for `mut arr`, because `Read` does not modify memory
let arr: [u8; 4] = [1, 2, 3, 4];
// `slice` needs to be `mut`, because `Read` will
// actually make the slice smaller with every step
let mut slice: &[u8] = &arr;
let mut read: &mut std::io::Read = &mut slice;https://stackoverflow.com/questions/37881254
复制相似问题