小编Ner*_*ury的帖子

如何以惯用/高效的方式将数据从Read + Seek转换为Write?

我想从输入文件中的随机位置获取数据,并将它们顺序输出到输出文件.优选地,没有不必要的分配.

这是我发现的一种解决方案:

use std::io::{ self, SeekFrom, Cursor, Read, Write, Seek };

#[test]
fn read_write() {
    // let's say this is input file
    let mut input_file = Cursor::new(b"worldhello");
    // and this is output file
    let mut output_file = Vec::<u8>::new();

    assemble(&mut input_file, &mut output_file).unwrap();

    assert_eq!(b"helloworld", &output_file[..]);
}

// I want to take data from random locations in input file
// and output them sequentially to output file
pub fn assemble<I, O>(input: &mut I, output: &mut O) -> Result<(), io::Error> 
    where I: …
Run Code Online (Sandbox Code Playgroud)

io pipe rust

8
推荐指数
1
解决办法
626
查看次数

标签 统计

io ×1

pipe ×1

rust ×1