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

Literals don't live long enough #18852

Closed
mahkoh opened this issue Nov 10, 2014 · 4 comments
Closed

Literals don't live long enough #18852

mahkoh opened this issue Nov 10, 2014 · 4 comments

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Nov 10, 2014

fn main() {
    let x = "".as_slice();
}
test5.rs:2:13: 2:15 error: borrowed value does not live long enough
test5.rs:2     let x = "".as_slice();
                       ^~
test5.rs:1:11: 3:2 note: reference must be valid for the block at 1:10...
test5.rs:1 fn main() {
test5.rs:2     let x = "".as_slice();
test5.rs:3 }
test5.rs:2:5: 2:26 note: ...but borrowed value is only valid for the statement at 2:4
test5.rs:2     let x = "".as_slice();
               ^~~~~~~~~~~~~~~~~~~~~
test5.rs:2:5: 2:26 help: consider using a `let` binding to increase its lifetime
test5.rs:2     let x = "".as_slice();
               ^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

Similarly you can't pass an array literal to BufReader.

@mahkoh
Copy link
Contributor Author

mahkoh commented Nov 15, 2014

let x: &'static [u8] = [0] doesn't work either.

@steveklabnik
Copy link
Member

This now compiles without error.

@steveklabnik
Copy link
Member

The first one compiles, but the second one is trying to assign a static lifetime to a slice on the stack, which is invalid.

@mahkoh
Copy link
Contributor Author

mahkoh commented Jan 27, 2015

The correct workaround for the second one is to use const y: &'static [u8] = &[0];

lnicola added a commit to lnicola/rust that referenced this issue Jan 7, 2025
fix: Fix a bug that was caused by fixup reversing
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

2 participants