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

Document const {} syntax for std::thread_local. #110620

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,28 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// thread_local! {
/// pub static FOO: RefCell<u32> = RefCell::new(1);
///
/// #[allow(unused)]
/// static BAR: RefCell<f32> = RefCell::new(1.0);
/// }
/// # fn main() {}
///
/// FOO.with(|foo| assert_eq!(*foo.borrow(), 1));
/// BAR.with(|bar| assert_eq!(*bar.borrow(), 1.0));
/// ```
///
/// This macro supports a special `const {}` syntax that can be used
/// when the initialization expression can be evaluated as a constant.
/// This can enable a more efficient thread local implementation that
/// can avoid lazy initialization. For types that do not
/// [need to be dropped][crate::mem::needs_drop], this can enable an
/// even more efficient implementation that does not need to
/// track any additional state.
///
/// ```
/// use std::cell::Cell;
/// thread_local! {
/// pub static FOO: Cell<u32> = const { Cell::new(1) };
/// }
///
/// FOO.with(|foo| assert_eq!(foo.get(), 1));
/// ```
///
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
Expand Down