So, in:
fn v1<'a> (a:~[&'a str]) -> ~[&'a str] {
return a;
}
#[test]
fn test_can_create_struct() {
let x = v1("Hello World".split(' ').collect());
}
I know, I've read http://static.rust-lang.org/doc/master/guide-lifetimes.html#named-lifetimes, but I don't understand what this code actually does.
The function is basically parametrized like a generic fn but with a lifetime, is what I've seen said on the IRC channel, but lets imagine that is the case, and we have a L, which is some specific lifetime structure.
Apparently I'm implicitly calling:
v1::<L>("Hello World".split(' ').collect());
..but I'm not. The lifetime being passed to this function is an instance of a lifetime, its not a TYPE of lifetime, so that comment doesn't make any sense to me.
I mean, I basically understand whats going on (I think): The returned ~[&str]
has the same lifetime as the scope of the caller, presumably the test_can_create_struct()
function. That's because (as I understand it) the function v1
is invoked with the lifetime instance from the calling function.
Very confusing.
Then we have some other examples like: https://gist.github.com/bvssvni/8970459
Here's a fragment:
impl<'a> Data<'a> {
pub fn new() -> Data<'a> {
Data { a: None, b: None }
}
pub fn a(&'a mut self, a: int) -> State<'a, Step1> {
self.a = Some(a);
State { data: self }
}
}
Now here I naively assumed that the Data<'a>
means that the lifetime instance for the function a()
is the same.
i.e. If you create a Data
(let blah = Data::new()
) and call blah.a()
, the lifetime is inherited from the create call; i.e. the State
object returned will exist for as long as the parent Data
object does.
...but apparently that's wrong too. So I have just no idea what the lifetime variables mean at all now.
Help!