To get a sample of 5 elements without replacement from an array in Rust, we can use the rand
crate and its seq::index::sample
method. Here's an example:
main.rs363 chars16 lines
Here, we first create an array with 10 integers. Then, we use the sample
function to generate a sample of 5 indices without replacement, using the length of the array as the population size. Finally, we create a new vector and populate it with the elements of the array at the sampled indices. The resulting vector contains the 5 randomly sampled elements from the array.
gistlibby LogSnag