Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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