Skip to content

Instantly share code, notes, and snippets.

@aleics
Created August 22, 2016 08:46
Show Gist options
  • Save aleics/a8d450709763534229058158b9f03701 to your computer and use it in GitHub Desktop.
Save aleics/a8d450709763534229058158b9f03701 to your computer and use it in GitHub Desktop.
How to properly and efficiently read a file in Rust
fn file_to_bytes(path: &Path) -> Result<Vec<u8>, std::io::Error> {
File::open(path).and_then(|mut file| {
let mut bytes = Vec::new();
try!(file.read_to_end(&mut bytes));
Ok(bytes)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment