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

impl Trait causes "borrowed value does not live long enough" #50823

Open
cuviper opened this Issue May 17, 2018 · 1 comment

Comments

Projects
None yet
5 participants
@cuviper
Copy link
Member

cuviper commented May 17, 2018

This code:

fn main() {
    println!("{:?}", max());
}

fn max() -> Option<i32> {
    let v = vec![1, 2, 3];
    iter(&v).max()
}

fn iter(a: &[i32]) -> impl Iterator<Item = i32> + '_ {
    a.iter().cloned()
}

gives this error:

error[E0597]: `v` does not live long enough
 --> src/main.rs:7:11
  |
7 |     iter(&v).max()
  |           ^ borrowed value does not live long enough
8 | }
  | - `v` dropped here while still borrowed
  |
  = note: values in a scope are dropped in the opposite order they are created

But if I change the return type to an explicit std::iter::Cloned<std::slice::Iter<'_, i32>>, then it's fine. It's also fine if I bind that result before returning it, like: let m = iter(&v).max(); m

And it works with #![feature(nll)], so maybe I just have to hope for that to stabilize soon... 🙏

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Sep 13, 2018

BTW NLL is turned on as part of the 2018 edition, so if you upgrade to that, this example will start working: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment