0

Suppose I had the following variable:

let mut n: &[((usize, usize), (usize, usize))];

I'd like to shuffle some data in this variable. I tried:

rng.shuffle(&mut n); // rng is of type rand::Rng

which of course leads to a compiler error complaining that the trait RandCore isn't implemented for that type. I don't really mind implementing it, but I'd hate to have to define a trait implementation for every variation of that type (e.g. (usize, isize), ((usize, isize), (usize, isize), (isize, usize)), etc) that I have in my code.

Is there another way of "automatically" defining this trait (like using macros of some sort)?

Kied Llaentenn
  • 133
  • 1
  • 11
  • 1
    In addition to the ordering problem pointed out in the answer, perhaps you need to explicitly `use rand::seq::SliceRandom` to get the `RandCore` trait implemented for slices. – user4815162342 Aug 13 '20 at 20:25
  • See the answers here for how to properly shuffle a slice: [How do I create a Vec from a range and shuffle it?](https://stackoverflow.com/questions/26033976/how-do-i-create-a-vec-from-a-range-and-shuffle-it) – Herohtar Aug 13 '20 at 20:32

1 Answers1

3

You have the arguments the wrong way round. It should be n.shuffle(&mut rng). RandCore is the trait for an rng, not a value to be shuffled. You should then find SliceRandom is implemented for all slices if you use it.

user1937198
  • 4,987
  • 4
  • 20
  • 31