Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Vec<String> as IntoIterator<&'a str>

I have a function that is supposed to pick random words from a list of words:

pub fn random_words<'a, I, R>(rng: &mut R, n: usize, words: I) -> Vec<&'a str>
where
    I: IntoIterator<Item = &'a str>,
    R: rand::Rng,
{
    rand::sample(rng, words.into_iter(), n)
}

Presumably that's a reasonable signature: Since I don't actually need the string itself in the function, working on references is more efficient than taking a full String.

How do I elegantly and efficiently pass a Vec<String> with words that my program reads from a file to this function? I got as far as this:

extern crate rand;

fn main() {
    let mut rng = rand::thread_rng();
    let wordlist: Vec<String> = vec!["a".to_string(), "b".to_string()];

    let words = random_words(&mut rng, 4, wordlist.iter().map(|s| s.as_ref()));
}

Is that the proper way? Can I write this without explicitly mapping over the list of words to get a reference?

like image 935
lunaryorn Avatar asked Feb 25 '16 11:02

lunaryorn


1 Answers

You can change your generic function to take anything that can be turned into a &str instead of having it take an iterator that yields a &str:

pub fn random_words<'a, I, R, J>(rng: &mut R, n: usize, words: I) -> Vec<&'a str>
where
    I: IntoIterator<Item = &'a J>,
    J: AsRef<str> + 'a,
    R: rand::Rng,
{
    rand::sample(rng, words.into_iter().map(AsRef::as_ref), n)
}
let words: Vec<&str> = random_words(&mut rng, 4, &wordlist);

The Book even has an entire chapter devoted to this topic

like image 166
oli_obk Avatar answered Sep 28 '22 06:09

oli_obk