docs(LazyLock): add example pass local LazyLock variable to struct

Signed-off-by: DragonBillow <DragonBillow@outlook.com>
This commit is contained in:
DragonBillow 2023-07-25 12:21:30 +08:00
parent beef07fe8f
commit 40dd5a337c
No known key found for this signature in database
GPG Key ID: 7EA203FF0732DAFB

View File

@ -25,6 +25,8 @@ union Data<T, F> {
///
/// # Examples
///
/// Initialize static variables with `LazyLock`.
///
/// ```
/// #![feature(lazy_cell)]
///
@ -54,6 +56,24 @@ union Data<T, F> {
/// // Some("Hoyten")
/// }
/// ```
/// Initialize fields with `LazyLock`.
/// ```
/// #![feature(lazy_cell)]
///
/// use std::sync::LazyLock;
///
/// #[derive(Debug)]
/// struct UseCellLock {
/// number: LazyLock<u32>,
/// }
/// fn main() {
/// let lock: LazyLock<u32> = LazyLock::new(|| 0u32);
///
/// let data = UseCellLock { number: lock };
/// println!("{}", *data.number);
/// }
/// ```
#[unstable(feature = "lazy_cell", issue = "109736")]
pub struct LazyLock<T, F = fn() -> T> {
once: Once,