0

K is an i64 and points is a Vec<Vec<i64>> that is used to represent points in an n-dim space.

let mut centroids: Vec<Vec<i64>> = 
    rand::seq::sample_iter(&mut rand::thread_rng(), 
                           points.iter(), 
                           K as usize).unwrap();

Is rand::thread_rng() moved into the function scope and thus gotten rid of at the end of the function? (making the whole thing not leak memory)

If not, is there a way to do so without declaring it separately? What led me to this question is the way the drop function works since it pretty much employs the same idea.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Rares Dima
  • 1,575
  • 1
  • 15
  • 38
  • Additionally, you will get *far* better answers if you provide a [MCVE]. For example, instead of using prose to tell us what `K` and `points` are, show the code for them. You can even write a function that accepts `points` so you don't have to actually define it. – Shepmaster Feb 05 '18 at 19:27
  • 1
    Consider sticking to one question per post, as they do not seem to be sufficiently related. I would also advise you to read the respective documentation for [`rand::thread_rng`](https://docs.rs/rand/0.4.2/rand/fn.thread_rng.html), which should just about answer the first question. – E_net4 Feb 05 '18 at 19:28
  • 1
    No, the value of `rand::thread_rng()` is not moved into the function, although the mutable reference to the value is. The RNG value itself is destroyed at the end of the statement. – Shepmaster Feb 05 '18 at 19:55
  • 1
    @Shepmaster The output of `thread_rng()` is, in turn, a reference-counted pointer to a thread-local value. The inner RNG value is not destroyed, but the Rc is. :) – E_net4 Feb 05 '18 at 20:00

0 Answers0