Struct std::lazy::Lazy[−][src]

pub struct Lazy<T, F = fn() -> T> { /* fields omitted */ }
🔬 This is a nightly-only experimental API. (once_cell #74465)
Expand description

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

Examples

#![feature(once_cell)]

use std::lazy::Lazy;

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

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

Implementations

🔬 This is a nightly-only experimental API. (once_cell #74465)

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

Examples

#![feature(once_cell)]

use std::lazy::Lazy;

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

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

assert_eq!(&*lazy, "HELLO, WORLD!");
Run
🔬 This is a nightly-only experimental API. (once_cell #74465)

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

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

Examples

#![feature(once_cell)]

use std::lazy::Lazy;

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

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

Trait Implementations

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

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

解引用后的结果类型。

解引用值。

Auto Trait Implementations

Blanket Implementations

获取 self 的 TypeId。 Read more

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

从拥有的值中借用。 Read more

执行转换。

执行转换。

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

执行转换。

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

执行转换。