You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tonight I was told on the IRC channel that all of the lines with comments should be valid rust code but two fail. I was told it might be an issue with the borrow checker.
fnfind<'r,T:Eq>(data:&'r Vec<T>,key:&T) -> Option<&'r T>{
data
.iter().find(|& x| x == key )//.map(|& ref v| v) // Does NOT compile -> Borrowed value does not live long enough//.map(|& ref v| -> &'r T {v}) // does NOT compile -> Borrowed value does not live long enough//.map(|& ref v: &'r T| -> &'r T {v}) // compiles.map(|&ref v:&'r T| {v})// compiles}
I'm sorry I can't tell you anymore since I'm still new to rust.
I used the following rustc version: 0.11-pre-nightly (d35804e 2014-04-18 00:01:22 -0700)
The text was updated successfully, but these errors were encountered:
cc @nikomatsakis@pnkfelix
I've seen similar behavior around iterators and closures, is this related in any way to method lookup improvements?
@chridou TBQH, few people actually know the distinction, but the borrow checker is doing its job perfectly here - the issue is in the region checker, which does lifetime inference, and in this case the lifetimes it assigns are shorter than they could be.
This doesn't show up in regular functions as they require a complete signature, but closures can omit types (and lifetimes), making them vulnerable to inaccurate inference.
Even worse, you might not be able to name the input/output lifetimes (if they're temporaries in the outer function, for example) to work around such an issue, and can end up needing a function generic over the lifetime - there's syntax for generic lifetime types, but not expressions, AFAIK:
<'a> |&'a T| -> &'a U// returns a reference with the same lifetime as its input
chridou
changed the title
Possible defect in borrow checker
Possible defect in region checker
Apr 22, 2014
Tonight I was told on the IRC channel that all of the lines with comments should be valid rust code but two fail. I was told it might be an issue with the borrow checker.
I'm sorry I can't tell you anymore since I'm still new to rust.
I used the following rustc version: 0.11-pre-nightly (d35804e 2014-04-18 00:01:22 -0700)
The text was updated successfully, but these errors were encountered: