From 3c3d20b3d913f4fa6d7d705e81c26191189f2efc Mon Sep 17 00:00:00 2001 From: foosinn Date: Tue, 30 Nov 2021 03:21:27 +0100 Subject: [PATCH] avoid allocations for results --- src/main.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2aa1996..8fc4e03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ struct App { impl App { /// check if word consists only of letters in query - fn check_word(&self, word: &str) -> Option { + fn check_word<'a>(&self, word: &'a str) -> Option<&'a str> { let mut query = self.query.clone(); word.chars() .try_for_each(|c| { @@ -19,17 +19,19 @@ impl App { query.remove(position); Some(()) }) - .and(Some(word.to_string())) + .and(Some(word)) } } fn main() { let app: App = argh::from_env(); + let start = std::time::Instant::now(); let mut results = WORDS .lines() .filter_map(|line| app.check_word(line)) - .collect::>(); + .collect::>(); + eprintln!("search took: {:?}", start.elapsed()); results.sort_by(|a, b| { a.len()