From 026db02c643aae8920e09468388a9935487fc809 Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 28 Oct 2018 11:33:03 +0100 Subject: [PATCH] removed pre-allocation of outputs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it doesn’t really seem to affect the performance anyway. vec.push() is pretty good --- src/main.rs | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2d423be..602aac7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,23 +12,11 @@ fn main() -> Result<()> { 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 mut outputs = Vec::new(); 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 { @@ -37,16 +25,10 @@ fn main() -> Result<()> { println!("“{}” remains unchanged", entry); } */ - outputs[i] = new; - i += 1; + outputs.push(new); } - /*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(()) }