Skip to content

Instantly share code, notes, and snippets.

@grahamking
Created April 26, 2013 17:42
Show Gist options
  • Save grahamking/5468983 to your computer and use it in GitHub Desktop.
Save grahamking/5468983 to your computer and use it in GitHub Desktop.
Rust: Load a file
fn load(filename: ~str) -> ~[~str] {
// The simple way:
// let read_result = io::file_reader(~path::Path(filename));
let read_result: Result<@Reader, ~str>;
read_result = io::file_reader(~path::Path(filename));
if read_result.is_ok() {
let file = read_result.unwrap();
return file.read_lines();
}
println(fmt!("Error reading file: %?", read_result.unwrap_err()));
return ~[];
}
fn main() {
let contents = load(~"myfile.txt");
println(fmt!("%?", contents));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment