RosettaCodeData/Task/Read-entire-file/Rust/read-entire-file.rust

16 lines
451 B
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
use std::fs::File;
use std::io::Read;
2014-04-02 16:56:35 +00:00
fn main() {
2015-11-18 06:14:39 +00:00
let mut file = File::open("somefile.txt").unwrap();
2015-02-20 00:35:01 -05:00
2015-11-18 06:14:39 +00:00
let mut contents: Vec<u8> = Vec::new();
// Returns amount of bytes read and append the result to the buffer
let result = file.read_to_end(&mut contents).unwrap();
println!("Read {} bytes", result);
// To print the contents of the file
let filestr = String::from_utf8(contents).unwrap();
println!("{}", filestr);
2014-04-02 16:56:35 +00:00
}