Skip to content

Commit

Permalink
document const-initialization for thread_local!
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMikh committed Apr 4, 2023
1 parent f4ec0e7 commit 0b23d47
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// # fn main() {}
/// ```
///
/// This macro also supports wrapping initializing expression in `const` block
/// if it can be evaluated in const context:
///
/// ```
/// use std::cell::Cell;
/// thread_local! {
/// static INIT_FLAG: Cell<bool> = const { Cell::new(false) };
/// }
/// ```
///
/// Doing so does not change the behavior, but might enable more efficient implementation
/// of [`LocalKey`][`std::thread::LocalKey`] on supported platforms.
///
/// If initializing expression can not be evaluated in const context, wrapping it in
/// `const` block is a compile error.
///
/// ```compile_fail
/// thread_local! {
/// static FOO: Vec<i32> = const { vec![0] };
/// }
/// ```
///
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
/// information.
///
Expand Down

0 comments on commit 0b23d47

Please sign in to comment.