Last active
August 2, 2016 12:23
-
-
Save tos-kamiya/69c88290024f147f3e31 to your computer and use it in GitHub Desktop.
Try to re-write code in http://dtrace.org/blogs/ahl/2015/06/22/first-rust-program-pain/
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
use std::fs::File; | |
use std::path::Path; | |
use std::error::Error; | |
use std::io::{BufRead, BufReader, stdin}; | |
use std::collections::HashMap; | |
fn sort_str(s: &str) -> String { | |
let mut v: Vec<char> = s.chars().collect(); | |
v.sort(); | |
v.into_iter().collect() | |
} | |
fn main() { | |
let path = Path::new("../word.lst"); | |
let file = match File::open(&path) { | |
Err(why) => panic!("failed to open {}: {}", path.display(), | |
why.description()), | |
Ok(f) => f, | |
}; | |
let mut dict: HashMap<String, Vec<String>> = HashMap::new(); | |
for line in BufReader::new(file).lines() { | |
let s = line.unwrap(); | |
dict.entry(sort_str(&s)).or_insert(Vec::new()).push(s); | |
} | |
let sin = stdin(); | |
for line in sin.lock().lines() { | |
let s = line.unwrap(); | |
if let Some(v) = dict.get(&sort_str(&s)) { | |
println!("anagrams for {}: {}", &s, &v.connect(" ")); | |
} else { | |
println!("no dice"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rust compiler v 1.3.0 warns "use of deprecated" for ".connect" in line 31.
Seems renamed to ".join".