Skip to content
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

in rust edition 2021, fourth-peek test code shows list does not live long enough #286

Open
dev-m1-macbook opened this issue Oct 24, 2023 · 3 comments

Comments

@dev-m1-macbook
Copy link

the following code errors with list does not live long enough error in rust 2021 edition

#[test]
fn peek() {
    let mut list = List::new();
    assert!(list.peek_front().is_none());
    list.push_front(1); list.push_front(2); list.push_front(3);

    assert_eq!(&*list.peek_front().unwrap(), &3);
    //                       --> `list` does not live long enough (borrowed value does not live long enough)
}

but the following code works:

#[test]
fn peek() {
    let mut list = List::new();
    assert!(list.peek_front().is_none());
    list.push_front(1); list.push_front(2); list.push_front(3);

    // not a good name
    let elem: i32 = list.peek_front().unwrap();
    assert_eq!(elem, 3);
}
@indirection42
Copy link

The snippet you posted would be ok. If you leave off the semicolon of the last expression, the compiler will complain.

@aresbit
Copy link

aresbit commented Dec 27, 2023

test test::peek ... ok

@pobv
Copy link

pobv commented Aug 14, 2024

Shouldn't there be an additional *?

let elem: i32 = *list.peek_front().unwrap();

and do we really have to copy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants