I'm experimenting with Rust right now, and I'm getting really tripped up by all sorts of random compiler errors like this one:
error: cannot move out of dereference of `&`-pointer
return list[idx];
^~~~~~~~~
The snippet in question is reproduced below. list is a word list, and the idea is to return a random word from list which is within a certain length (between minLength and maxLength). So, I generate a random uint and constrain it to the size of the list using a modulo. Then I want to check to see if that word is the right length, and if it is return it.
Obviously this isn't the most efficient way to do this, but I just wanted something easy that would fulfill the needs of the exercise.
fn get_a_word(list: Vec<String>, minLength: uint, maxLength: uint) -> String {
loop {
let idx = rand::random::<uint>() % list.len();
if list[idx].len() > minLength && list[idx].len() < maxLength {
return list[idx];
}
}
}
I've done some research into what this error message means, but everyone seems to say that this comes up when you're dealing with Option<T> retvals, not attempting to get the value out of a collection.
This is probably a really simple question, but why on earth doesn't Rust just let you return a value from a collection/vector/array?