The only way I know reading up to n bytes from file is this:
use std::fs::File;
use std::io;
use std::io::prelude::*;
fn main() -> io::Result<()> {
let mut f = File::open("foo.txt")?;
let mut buffer = [0; 10];
// read exactly 10 bytes
f.read_exact(&mut buffer)?;
Ok(())
}
What I would like to implement is: user gives a buffer and the parameter n
, I put up to n
bytes into that buffer.
fn read_n_bytes(f: &File, n: usize, dst: &mut Vec<u8>) {
let mut buffer = [0; n]; // error here, n isn't a constant
f.read_exact(&mut buffer);
// concat content of buffer to dst...
}
How to I tell read_exact to read up to n bytes into dst
directly?
fn read_n_bytes(f: &File, n: usize, dst: &mut Vec<u8>) {
f.read_exact(dst, n);
}