Skip to content

Commit 715e797

Browse files
matthiaskrgrgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#133498 - GuillaumeGomez:missing-examples, r=joboet
Add missing code examples on `LocalKey` r? ``@Amanieu``
2 parents 7856647 + d03dba3 commit 715e797

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

std/src/thread/local.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ impl<T: 'static> LocalKey<T> {
252252
/// This function will `panic!()` if the key currently has its
253253
/// destructor running, and it **may** panic if the destructor has
254254
/// previously been run for this thread.
255+
///
256+
/// # Examples
257+
///
258+
/// ```
259+
/// thread_local! {
260+
/// pub static STATIC: String = String::from("I am");
261+
/// }
262+
///
263+
/// assert_eq!(
264+
/// STATIC.with(|original_value| format!("{original_value} initialized")),
265+
/// "I am initialized",
266+
/// );
267+
/// ```
255268
#[stable(feature = "rust1", since = "1.0.0")]
256269
pub fn with<F, R>(&'static self, f: F) -> R
257270
where
@@ -273,6 +286,19 @@ impl<T: 'static> LocalKey<T> {
273286
///
274287
/// This function will still `panic!()` if the key is uninitialized and the
275288
/// key's initializer panics.
289+
///
290+
/// # Examples
291+
///
292+
/// ```
293+
/// thread_local! {
294+
/// pub static STATIC: String = String::from("I am");
295+
/// }
296+
///
297+
/// assert_eq!(
298+
/// STATIC.try_with(|original_value| format!("{original_value} initialized")),
299+
/// Ok(String::from("I am initialized")),
300+
/// );
301+
/// ```
276302
#[stable(feature = "thread_local_try_with", since = "1.26.0")]
277303
#[inline]
278304
pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
@@ -452,7 +478,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
452478
/// Panics if the key currently has its destructor running,
453479
/// and it **may** panic if the destructor has previously been run for this thread.
454480
///
455-
/// # Example
481+
/// # Examples
456482
///
457483
/// ```
458484
/// use std::cell::RefCell;
@@ -483,7 +509,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
483509
/// Panics if the key currently has its destructor running,
484510
/// and it **may** panic if the destructor has previously been run for this thread.
485511
///
486-
/// # Example
512+
/// # Examples
487513
///
488514
/// ```
489515
/// use std::cell::RefCell;

0 commit comments

Comments
 (0)