Skip to content

Commit

Permalink
perf: optimize getting random quote
Browse files Browse the repository at this point in the history
building new vec of quotes for it to be selected one at random from is
overhead, building new vec is required only in case of filtering, for
example, by character
  • Loading branch information
dmyTRUEk committed Mar 29, 2024
1 parent 82bb1dd commit ade210e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ fn main() -> Result<(), &'static str> {
}

fn get_random_quote(char: Option<String>) -> Result<&'static Quote, &'static str> {
let quotes: Vec<&Quote> = match char {
None => QUOTES.iter().collect(),
Some(char) => QUOTES.iter()
.filter(|quote| quote.char.to_str().to_lowercase().contains(&char.to_lowercase()))
.collect()
};
if quotes.len() == 0 { return Err("Character or her quote not found.") }
let mut rng = thread_rng();
let random_quote_index: usize = rng.gen_range(0..quotes.len());
Ok(quotes[random_quote_index])
match char {
None => {
let random_quote_index: usize = rng.gen_range(0..QUOTES.len());
Ok(&QUOTES[random_quote_index])
}
Some(char) => {
let char_lowercase: String = char.to_lowercase();
let quotes: Vec<&Quote> = QUOTES.iter()
.filter(|quote| quote.char.to_str().to_lowercase().contains(&char_lowercase))
.collect();
if quotes.len() == 0 { return Err("Character's quote not found.") }
let random_quote_index: usize = rng.gen_range(0..quotes.len());
Ok(quotes[random_quote_index])
}
}
}

0 comments on commit ade210e

Please sign in to comment.