Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2015 16:19
Show Gist options
  • Save anonymous/ea04b8ecf30d8ef36d06 to your computer and use it in GitHub Desktop.
Save anonymous/ea04b8ecf30d8ef36d06 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::collections::HashMap;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
use std::error::Error;
use std::mem;
use std::str;
type Something<'a> = HashMap<&'a str, &'a str>;
fn getLines(filename: &str) -> Result<io::Lines<BufReader<File>>, io::Error> {
let mut f = try!(File::open(filename));
Ok(BufReader::new(f).lines())
}
fn getSomething<'a>() -> Result<&'a Something<'a>, io::Error> {
let mut something = Something::new();
try!(getLines("gates.txt"))
.map(|line| line.unwrap().as_ref())
.map(|line| something.insert(line, line));
Ok(&something)
}
fn main() {
match getSomething() {
Ok(something) => println!("Values: (yay, {})", something.get("yay").unwrap()),
Err(e) => println!("Error: {}", e.to_string()),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment