Reputation: 751
What are the differences between these two functions in rust?
fn foo<'a>(x: &'a i32) -> &'a i32 {
x
}
fn foo<'a: 'b, 'b>(x: &'a i32) -> &'b i32 {
x
}
What constraints are provided when the same lifetime parameter 'a
is used among multiple function parameters and also in the return value?
fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
...
}
Upvotes: 2
Views: 144
Reputation: 71450
Two differences as far as I can tell:
let f: fn(&i32) -> &i32 = foo2;
does not compile).You almost always want (1), since it is less verbose and allows HRTB.
2.
The lifetime is the smallest of all. Ignoring the points at question 1, this is the same as:
fn foo<'a, 'b: 'a, 'c: 'a>(x: &'b i32, y: &'c i32) -> &'a i32 {
...
}
Upvotes: 3