Skip to content

Commit 0b23d47

Browse files
committed
document const-initialization for thread_local!
1 parent f4ec0e7 commit 0b23d47

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

library/std/src/thread/local.rs

+22
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
137137
/// # fn main() {}
138138
/// ```
139139
///
140+
/// This macro also supports wrapping initializing expression in `const` block
141+
/// if it can be evaluated in const context:
142+
///
143+
/// ```
144+
/// use std::cell::Cell;
145+
/// thread_local! {
146+
/// static INIT_FLAG: Cell<bool> = const { Cell::new(false) };
147+
/// }
148+
/// ```
149+
///
150+
/// Doing so does not change the behavior, but might enable more efficient implementation
151+
/// of [`LocalKey`][`std::thread::LocalKey`] on supported platforms.
152+
///
153+
/// If initializing expression can not be evaluated in const context, wrapping it in
154+
/// `const` block is a compile error.
155+
///
156+
/// ```compile_fail
157+
/// thread_local! {
158+
/// static FOO: Vec<i32> = const { vec![0] };
159+
/// }
160+
/// ```
161+
///
140162
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
141163
/// information.
142164
///

0 commit comments

Comments
 (0)