0

I have an input file opened as a BufReader in Rust. Now I'd like to copy a specific range of bytes into a new output file. What would be the idiomatic way to do this in Rust v1.24?

use std::io::{self, BufReader, BufWriter, SeekFrom, Seek};
use std::fs::File;

fn copy_file_range(input_file: &str, output_file: &str, offset: u64, length: u64) -> io::Result<bool> {
    let f = File::open(input_file).expect("Cannot open input file");
    let mut reader = BufReader::new(f);
    let f = File::create(output_file).expect("Cannot create output file");
    let mut writer = BufWriter::new(f);

    reader.seek(SeekFrom::Start(offset))?;
    // ...

    Ok(true)
}
Boiethios
  • 38,438
  • 19
  • 134
  • 183
blerontin
  • 2,892
  • 5
  • 34
  • 60
  • 1
    The dupe applied to this problem: you want a combination of `io::copy` and `take`, something like this: `io::copy(&mut input.take(length), &mut output)` – E_net4 Jul 25 '18 at 10:18
  • 1
    @E_net4 Indeed using `io::copy` together with `seek` and `take` on the reader was the solution! – blerontin Jul 25 '18 at 10:25

0 Answers0