[rust-dev] Simplifying lifetime expression

Paulo Sérgio Almeida psa at di.uminho.pt
Mon Apr 22 11:10:05 PDT 2013


On 4/22/13 5:48 PM, Diggory Hardy wrote:
> Please don't use HTML when posting to lists. 
Sorry. I hope this one goes in plain text. (Added a text domain to 
Thunderbird.)
>> 2) function parameter types or return type that are not type parameters have
>> all the same implicit lifetime by default, but they can be qualified
>> explicitly with some lifetime.
> That would mean that a function with default-lifetime parameters taking two
> borrowing references could not be called when the two parameters have different
> lifetime, if I understand correctly (though I am not aware of the whole
> situation). Example:
>
> fn f<'l>(x : & 'l int, y : & 'l int) -> & 'l int {
>      x
> }
This case would be written, with the same meaning, as:

fn f(x : &int, y : &int) -> &int {
     x
}

>
> fn caller<'l>(x : & 'l int) -> & 'l int {
>      let y = 2;
>      f(x, &y)
> }

fn caller(x : &int) -> &int {
     let y = 2;
     f(x, &y)
}

>
> fn main() {
>      let x = 5;
>      io::println(fmt!("%i", *caller(&x)));
> }
>
> (This fails, but giving parameter y an independent lifetime makes it succeed.)
And would fail for the same reason. We would want to write, in the new 
proposal:

fn f(x : &int, y : &'int) -> &int {
     x
}

or

fn f(x : &'int, y : &int) -> &'int {
     x
}

and

fn caller(x : &int) -> &int {
     let y = 2;
     f(x, &y)
}

And it would work as desired with quite less noise (with just one or two 
' more than the plain version without lifetimes).

Regards,
Paulo
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



More information about the Rust-dev mailing list