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

Add a warning in the FAQ about const vs static #145

Closed
jRimbault opened this issue Mar 6, 2021 · 3 comments
Closed

Add a warning in the FAQ about const vs static #145

jRimbault opened this issue Mar 6, 2021 · 3 comments

Comments

@jRimbault
Copy link

jRimbault commented Mar 6, 2021

I think some people may have some misconceptions about what const and static actually means, this came up in this reddit thread today. I tried explaining shortly by analogy (I'm not the best explainer), the result of the constructor function is copy pasted everywhere the const result is used. Maybe this could warrant a reminder note in the FAQ part of the docs ?

@matklad
Copy link
Owner

matklad commented Mar 8, 2021

cc rust-lang/rust#40543

@mrozekma
Copy link

I just made this exact mistake (playground):

use once_cell::unsync::OnceCell; // 1.9.0

const CELL: OnceCell<bool> = OnceCell::new();

pub fn main() {
    assert_eq!(CELL.set(true), Ok(()));
    assert_eq!(CELL.set(true), Err(true)); // Fails
}

I assume based on the linked rust-lang conversation that there's no way for once_cell to catch this mistake and it would have to be a compiler change, but a warning in the FAQ might be worthwhile.

@dsherret
Copy link

dsherret commented Dec 17, 2022

I also just hit this. I wonder if perhaps there could be a debug panic if a lazy is initialized twice saying the user likley has it in a const by accident. Or maybe that’s not possible?

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