Any idea why the following fails
----
use std::{io,rand,task};
fn main() {
for ["Alice", "Bob", "Carol"].iter().advance |&name| {
do task::spawn {
use std::rand::RngUtil;
let v = rand::rng().shuffle([1, 2, 3]);
for v.iter().advance |&num| {
io::print(fmt!("%s says: '%d'\n", name, num))
}
}
}
}
----
with
----
hello.rs:4:8: 4:33 error: borrowed value does not live long enough
hello.rs:4 for ["Alice", "Bob", "Carol"].iter().advance |&name| {
^~~~~~~~~~~~~~~~~~~~~~~~~
hello.rs:12:4: 12:5 note: borrowed pointer must be valid for the method
call at 12:4...
hello.rs:12 }
^
hello.rs:4:8: 4:41 note: ...but borrowed value is only valid for the method
call at 4:8
hello.rs:4 for ["Alice", "Bob", "Carol"].iter().advance |&name| {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
----
Changing to
let lst = ["Alice", "Bob", "Carol"];
for lst.iter().advance |&name| {
//...
}
succeeds, but I am surprised that the lifetime is not equivalent for lst
and the raw vector literal for this specific code as there is nothing after
the for-loop.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev