Struct std::lazy::Lazy
pub struct Lazy<T, F = fn() -> T> { /* fields omitted */ }
A value which is initialized on the first access.
Examples
#![feature(once_cell)]
use std::lazy::Lazy;
let lazy: Lazy<i32> = Lazy::new(|| {
println!("initializing");
92
});
println!("ready");
println!("{}", *lazy);
println!("{}", *lazy);
// Prints:
// ready
// initializing
// 92
// 92Implementations
impl<T, F> Lazy<T, F>
pub const fn new(init: F) -> Lazy<T, F>
Creates a new lazy value with the given initializing function.
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!");
pub fn force(this: &Lazy<T, F>) -> &T
Forces the evaluation of this lazy value and returns a reference to the result.
This is equivalent to the Deref impl, but is explicit.
Examples
#![feature(once_cell)] use std::lazy::Lazy; let lazy = Lazy::new(|| 92); assert_eq!(Lazy::force(&lazy), &92); assert_eq!(&*lazy, &92);
Trait Implementations
pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the value using the given formatter. Read more
pub fn default() -> Lazy<T, fn() -> T>
Creates a new lazy value using Default as the initializing function.
type Target = T
The resulting type after dereferencing.
pub fn deref(&self) -> &T
Dereferences the value.
Auto Trait Implementations
impl<T, F = fn() -> T> !RefUnwindSafe for Lazy<T, F>
impl<T, F = fn() -> T> !Sync for Lazy<T, F>
impl<T, F> UnwindSafe for Lazy<T, F> where
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> From<T> for T
pub fn from(t: T) -> T
Performs the conversion.
pub fn into(self) -> U
Performs the conversion.
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
Performs the conversion.
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
Performs the conversion.
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/lazy/struct.Lazy.html