Buffer 获取字节数组的读取器

Buffer 获取字节数组的读取器,buffer,rust,reader,Buffer,Rust,Reader,我正在测试一些需要读卡器的代码。我有一个函数: fn next_byte<R: Read>(reader: &mut R) -> ... 但编译器不同意: the trait `std::io::Read` is not implemented for the type `[u8]` 为什么??我明确地说了&mut 使用rust 1.2.0您试图调用下一个字节::,但[u8]无法实现读取[u8]和&'a[u8]不是同一类型[u8]是一种无大小的数组类型,&'a[u8

我正在测试一些需要读卡器的代码。我有一个函数:

fn next_byte<R: Read>(reader: &mut R) -> ...
但编译器不同意:

the trait `std::io::Read` is not implemented for the type `[u8]`
为什么??我明确地说了
&mut


使用rust 1.2.0

您试图调用
下一个字节::
,但
[u8]
无法实现
读取
<代码>[u8]和
&'a[u8]
不是同一类型<代码>[u8]是一种无大小的数组类型,
&'a[u8]
是一个片

在片上使用
Read
实现时,需要对片进行变异,以便从上一次读取结束后恢复下一次读取。因此,需要将可变借用传递给切片

下面是一个简单的工作示例:

use std::io::Read;

fn next_byte<R: Read>(reader: &mut R) {
    let mut b = [0];
    reader.read(&mut b);
    println!("{} ", b[0]);
}

fn main() {
    let mut v = &[1u8, 2, 3] as &[u8];
    next_byte(&mut v);
    next_byte(&mut v);
    next_byte(&mut v);
}
使用std::io::Read;
fn下一个字节(读卡器:&mut R){
设mut b=[0];
reader.read(&mutb);
println!(“{}”,b[0]);
}
fn main(){
设mut v=&[1u8,2,3]为&[u8];
下一个字节(&mut v);
下一个字节(&mut v);
下一个字节(&mut v);
}
use std::io::Read;

fn next_byte<R: Read>(reader: &mut R) {
    let mut b = [0];
    reader.read(&mut b);
    println!("{} ", b[0]);
}

fn main() {
    let mut v = &[1u8, 2, 3] as &[u8];
    next_byte(&mut v);
    next_byte(&mut v);
    next_byte(&mut v);
}