Struct std::cell::LazyCell

source ·
pub struct LazyCell<T, F = fn() -> T> { /* private fields */ }
🔬This is a nightly-only experimental API. (lazy_cell #109736)
Expand description

在首次访问时初始化的值。

有关此结构体的线程安全版本,请参见 std::sync::LazyLock

Examples

#![feature(lazy_cell)]

use std::cell::LazyCell;

let lazy: LazyCell<i32> = LazyCell::new(|| {
    println!("initializing");
    92
});
println!("ready");
println!("{}", *lazy);
println!("{}", *lazy);

// Prints:
//   准备初始化
//   92
//   92
Run

Implementations§

source§

impl<T, F> LazyCell<T, F>where F: FnOnce() -> T,

source

pub const fn new(f: F) -> LazyCell<T, F>

🔬This is a nightly-only experimental API. (lazy_cell #109736)

使用给定的初始化函数创建一个新的惰性值。

Examples
#![feature(lazy_cell)]

use std::cell::LazyCell;

let hello = "Hello, World!".to_string();

let lazy = LazyCell::new(|| hello.to_uppercase());

assert_eq!(&*lazy, "HELLO, WORLD!");
Run
source

pub fn into_inner(this: LazyCell<T, F>) -> Result<T, F>

🔬This is a nightly-only experimental API. (lazy_cell_consume #109736)

使用此 LazyCell 返回存储的值。

如果 Lazy 已初始化,则返回 Ok(value),否则返回 Err(f)

Examples
#![feature(lazy_cell)]
#![feature(lazy_cell_consume)]

use std::cell::LazyCell;

let hello = "Hello, World!".to_string();

let lazy = LazyCell::new(|| hello.to_uppercase());

assert_eq!(&*lazy, "HELLO, WORLD!");
assert_eq!(LazyCell::into_inner(lazy).ok(), Some("HELLO, WORLD!".to_string()));
Run
source

pub fn force(this: &LazyCell<T, F>) -> &T

🔬This is a nightly-only experimental API. (lazy_cell #109736)

强制对此延迟值求值,并向结果返回引用。

这等效于 Deref impl,但是是显式的。

Examples
#![feature(lazy_cell)]

use std::cell::LazyCell;

let lazy = LazyCell::new(|| 92);

assert_eq!(LazyCell::force(&lazy), &92);
assert_eq!(&*lazy, &92);
Run

Trait Implementations§

source§

impl<T, F> Debug for LazyCell<T, F>where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。 Read more
source§

impl<T> Default for LazyCell<T, fn() -> T>where T: Default,

source§

fn default() -> LazyCell<T, fn() -> T>

使用 Default 作为初始化函数创建一个新的惰性值。

source§

impl<T, F> Deref for LazyCell<T, F>where F: FnOnce() -> T,

§

type Target = T

解引用后的结果类型。
source§

fn deref(&self) -> &T

解引用值。

Auto Trait Implementations§

§

impl<T, F = fn() -> T> !RefUnwindSafe for LazyCell<T, F>

§

impl<T, F> Send for LazyCell<T, F>where F: Send, T: Send,

§

impl<T, F = fn() -> T> !Sync for LazyCell<T, F>

§

impl<T, F> Unpin for LazyCell<T, F>where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for LazyCell<T, F>where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

发生转换错误时返回的类型。
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

发生转换错误时返回的类型。
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。