从u8切片创建Read trait对象

时间:2016-06-17 12:10:44

标签: rust slice trait-objects

我尝试从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

此代码有什么问题?

1 个答案:

答案 0 :(得分:6)

正如错误消息告诉您的那样,Read的{​​{1}} impl。我没有理由为&[u8]提供Read impl,因此您只需删除代码中的部分&mut[u8]

mut