I wanted to create a structwith a field containing a Vec<ThreadRng>.
So this works fine, I have a list of generators stored in Foo.generators:
extern crate rand;
use std::vec::Vec;
use rand::{Rng, ThreadRng};
struct Foo {
generators: Vec<ThreadRng>,
}
impl Foo {
fn new() -> Foo {
Foo { generators: vec![rand::thread_rng(), rand::thread_rng()]}
}
}
Now I would like to use it, say in a bar method:
impl Foo {
fn bar(&self) -> i32 {
self.generators[0].gen::<i32>()
}
}
But that I can't, and I get a cannot borrow immutable field 'self.generators' as mutable.
As I understand I cannot use the gen method of ThreadRng because it requires a mutable reference to the RNG (gen<T: Rand>(&mut self)) and since the definition of my field Foo.generators "Vec<ThreadRng>" doesn't specify that the ThreadRng should be mutable, I can't do anything with it that requires mutability.
First question: is my understanding of what's happening correct, or am I completely wrong? If so, could someone explain to me what's really happening?
Second question: admitting that my understanding is correct, how am I supposed to allow actions that requires mutability on my ThreadRngs?
Thanks in advance!