use std::fs::File; use std::io::{BufRead, BufReader, Result}; use std::collections::HashMap; use std::fs; fn main() -> Result<()> { let file = File::open("beets_mv3u_renames")?; let mut renames = HashMap::new(); for line in BufReader::new(file).lines() { let line = line?; let split: Vec<&str> = line.split("\t\t").collect(); renames.insert(split[0].to_string(), split[1].to_string()); } // get the number of lines in the file so we can pre-allocate the array let pf = fs::read_to_string("playlist.m3u").expect("unable to read"); let num_lines = pf.split("\n").collect::>().len() - 1; let mut outputs: Vec = vec![String::new(); num_lines]; //println!("{}", num_lines); let pls_file = File::open("playlist.m3u")?; let mut i = 0; for entry in BufReader::new(pls_file).lines() { let entry = entry?; /* match renames.get(&entry.to_string()) { Some(line) => println!("found {}", line), None => print!("") } */ let new = renames.get(&entry).unwrap_or(&entry).to_string(); /* if new != &entry { println!("renamed “{}” to “{}”", entry, new); } else { println!("“{}” remains unchanged", entry); } */ outputs[i] = new; i += 1; } /*for s in outputs { println!("{}", s); }*/ let data = outputs.join("\n"); fs::write("playlist2.m3u", data).expect("unable to write"); //println!("{}", pf); //println!("{}", renames.get(&"a string".to_string()).unwrap()); Ok(()) }