首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从u8切片创建读取特性对象

从u8切片创建读取特性对象
EN

Stack Overflow用户
提问于 2016-06-17 12:10:43
回答 1查看 1K关注 0票数 1

我正在尝试从一个Read切片创建一个u8特性对象,用于murmur3机箱中,如下所示

代码语言:javascript
运行
复制
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;
}

但我得到

代码语言:javascript
运行
复制
<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

这个密码怎么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-17 12:20:35

正如错误消息所告诉您的,&[u8]有一个&[u8]驱动程序。没有理由对Read进行&mut[u8]驱动,所以您只需删除代码中的一些mut

代码语言:javascript
运行
复制
// 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;
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37881254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档