Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get A Random Element From A Vec In Rust

This is how I'm getting random items from a Vec in Rust.

Code

```cargo
[dependencies]
rand = "0.8"
```

use rand::thread_rng;
use rand::seq::SliceRandom;

fn main() {
  let items = vec!["alfa", "bravo", "charlie", "delta"];
  let mut rng = thread_rng();
  match items.choose(&mut rng) {
    Some(item) => println!("{}", item),
    None => println!("No items in vec")
  }
}

Results

charlie