vendredi 10 septembre 2021

rust Vector sorted randomly

Given some rust vector, how to sort it (or create a new vector) with values sorted randomly?

fn main() {
  let mut v_ = Vec::<u32>::new();
  v_.push(1);
  v_.push(2);
  v_.push(3);
  v_.push(4);
  println!("{:?}", &v_);  // [1, 2, 3, 4]
  vr1 = randomize(&v_);
  println!("{:?}", &vr1);  // [1, 3, 4, 2]
  vr2 = randomize(&v_);
  println!("{:?}", &vr2);  // [3, 1, 2, 4]
}

What function could satisfy randomize ?

I can imagine some rudimentary approaches. However, I was hoping for a ready-made crate or standard library tools to do this cleanly.

This is for testing purposes.




Aucun commentaire:

Enregistrer un commentaire