Closed bstrie closed 7 months ago
Ah, here's an example:
fn foo(s: &str) -> &str {
let bar = |_| { s };
bar(&String::new())
}
The elision rules assign the same lifetime to the input and output of foo
, but if they were to do the same for bar
then the code would fail to compile, because the input to bar
and the output of bar
have different lifetimes.
I'll close this since it seems like you answered your own question.
From here: https://github.com/pretzelhammer/rust-blog/blob/master/posts/common-rust-lifetime-misconceptions.md#10-closures-follow-the-same-lifetime-elision-rules-as-functions
Would it be a breaking change? I'm struggling to come up with a example of a closure that currently compiles that would no longer compile if the elision rules were changed to match functions. A one-line example of such a closure would be illustrative and unobtrusive here. And in the event that the claim is mistaken, that would bode well for changing the language to remove this discrepancy.