diff options
| author | bors <bors@rust-lang.org> | 2023-11-30 15:21:09 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-30 15:21:09 +0000 |
| commit | d368c4043c26800bf1d45103e482d3e9d3bf6010 (patch) | |
| tree | 9500da2a806a8fa2c229e97350fe90c02bcd5f1d /library/std/src/sync | |
| parent | 33e8232b82efb5f57760fcf08045cb36b93ed8e4 (diff) | |
| parent | cd124409335f8830becb7f7ec7de1c6aa8a504e3 (diff) | |
Auto merge of #3197 - RalfJung:rustup, r=RalfJung
Rustup also move some fail tests into suitable subdirectories
Diffstat (limited to 'library/std/src/sync')
| -rw-r--r-- | library/std/src/sync/once_lock.rs | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs index f4963090795..52a43913243 100644 --- a/library/std/src/sync/once_lock.rs +++ b/library/std/src/sync/once_lock.rs @@ -13,22 +13,54 @@ use crate::sync::Once; /// /// # Examples /// +/// Using `OnceCell` to store a function’s previously computed value (a.k.a. +/// ‘lazy static’ or ‘memoizing’): +/// +/// ``` +/// use std::collections::HashMap; +/// use std::sync::OnceLock; +/// +/// fn hash_map() -> &'static HashMap<u32, char> { +/// static HASHMAP: OnceLock<HashMap<u32, char>> = OnceLock::new(); +/// HASHMAP.get_or_init(|| { +/// let mut m = HashMap::new(); +/// m.insert(0, 'a'); +/// m.insert(1, 'b'); +/// m.insert(2, 'c'); +/// m +/// }) +/// } +/// +/// // The `HashMap` is built, stored in the `OnceLock`, and returned. +/// let _ = hash_map(); +/// +/// // The `HashMap` is retrieved from the `OnceLock` and returned. +/// let _ = hash_map(); +/// ``` +/// +/// Writing to a `OnceLock` from a separate thread: +/// /// ``` /// use std::sync::OnceLock; /// -/// static CELL: OnceLock<String> = OnceLock::new(); +/// static CELL: OnceLock<usize> = OnceLock::new(); +/// +/// // `OnceLock` has not been written to yet. /// assert!(CELL.get().is_none()); /// +/// // Spawn a thread and write to `OnceLock`. /// std::thread::spawn(|| { -/// let value: &String = CELL.get_or_init(|| { -/// "Hello, World!".to_string() -/// }); -/// assert_eq!(value, "Hello, World!"); -/// }).join().unwrap(); +/// let value = CELL.get_or_init(|| 12345); +/// assert_eq!(value, &12345); +/// }) +/// .join() +/// .unwrap(); /// -/// let value: Option<&String> = CELL.get(); -/// assert!(value.is_some()); -/// assert_eq!(value.unwrap().as_str(), "Hello, World!"); +/// // `OnceLock` now contains the value. +/// assert_eq!( +/// CELL.get(), +/// Some(&12345), +/// ); /// ``` #[stable(feature = "once_cell", since = "1.70.0")] pub struct OnceLock<T> { |
