-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
References/borrows of const values don't have 'static
lifetime
#40522
Comments
This is expected. From the signature of Borrows will generally be scoped by their inner-most block (like the function body). You can, however, do this:
Edit: that said, it does seem like this could be made to work. We don't really need to know that |
@jmesmon Thanks for the solution! Actually I just solve my problem with this pattern. However I have a question: could we do inference here to give the reference a |
The problem here is static promotion. You can fix this by using static FOO: usize = 0;
fn bar() -> &'static usize {
&FOO
}
fn main() {
bar();
} Basically, constants are compile-time only so Note: The linked RFC has not been implemented (which is why this doesn't currently work). |
After reading the book, I think I know the point showed by @Stebalien 's code. Thank you all! |
Example:
Get error:
The text was updated successfully, but these errors were encountered: