How to shuffle a vector in Rust

1 Answer

0 votes
use rand::seq::SliceRandom;
use rand::thread_rng;

fn main() {
    let mut v: Vec<_> = (0..9).collect();
    
    println!("Unshuffled: {:?}", v);

    let mut rng = thread_rng();
    v.shuffle(&mut rng);

    println!("Shuffled: {:?}", v);
}




/*
run:

Unshuffled: [0, 1, 2, 3, 4, 5, 6, 7, 8]
Shuffled: [4, 0, 5, 3, 7, 2, 6, 1, 8]

*/

 



answered Jan 5, 2023 by avibootz
...