Created
April 26, 2013 18:20
-
-
Save grahamking/5469290 to your computer and use it in GitHub Desktop.
Rust: Load a file v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
match read_result { | |
Ok(file) => return file.read_lines(), | |
Err(e) => { | |
println(fmt!("Error reading file: %?", e)); | |
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