[rust-dev] Borrow lifetime assignment changed?

Ashish Myles marcianx at gmail.com
Wed Jul 3 19:53:04 PDT 2013


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20130703/582557da/attachment.html>


More information about the Rust-dev mailing list