Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2015 16:54
Show Gist options
  • Save anonymous/48d955b8b640697836d4 to your computer and use it in GitHub Desktop.
Save anonymous/48d955b8b640697836d4 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 = HashMap<String, String>;
fn get_line_vec(filename: &str) -> Result<&mut Vec<String>, io::Error> {
let mut vec: Vec<String> = Vec::new();
let mut f = try!(File::open(filename));
BufReader::new(f).lines()
.map(|line| vec.push(line.unwrap()));
Ok(&mut vec)
}
fn get_something() -> Result<Something, io::Error> {
let mut something = Something::new();
let mut lines = try!(get_line_vec("gates.txt"));
while let Some(line) = lines.pop() {
something.insert(line, line);
}
Ok(something)
}
fn main() {
match get_something() {
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