-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Worse than lexical borrowing #36403
Comments
It's not curious at all, just the compiler typechecking correctly. Let's change the function to explicit lifetimes: fn get_stream_mut<'a>(s: &'a mut String) -> Option<&'a mut String> {
unimplemented!()
}
fn get_or_create_stream<'a>(s: &'a mut String) -> &'a mut String {
{
match get_stream_mut(s) {
Some(stream) => return stream,
None => (),
};
};
new_request(s)
} What is the type of |
No, I've lent Actually, I've found a kind of workaround. Which does the same with runtime overhead: fn get_stream_mut(s: &mut String) -> Option<&mut String> {
unimplemented!()
}
fn new_request(s: &mut String) -> &mut String {
unimplemented!()
}
fn get_or_create_stream(s: &mut String) -> &mut String {
{
if get_stream_mut(s).is_some() {
return get_stream_mut(s).unwrap()
}
};
new_request(s)
} It typechecks OK. |
But you didn't, because then |
Sorry, I still don't understand.
If second call to |
Because the control flow ends at the `fn get_or_create_stream<'a>(s: &'a mut String) -> &'a mut String {
let b: bool = { get_stream_mut(s).is_some() };
if b {
get_stream_mut(s)
} else {
new_request(s)
}
} Or, in other words, |
It's a known limitation. It's discussed as part of Problem#3 in this blog post: Non-lexical Lifetimes: Introduction It's part of rust-lang/rfcs/issues/811 |
I'm going to close since this is a known limitation and I don't think it's helpful to track this; with NLL this should be fixed. |
Error is:
On stable and nightly.
Curiously, it works fine without first return.
The text was updated successfully, but these errors were encountered: