If the initialization closure passed to LazyCell::new panics, the cell will be poisoned.
Once the cell is poisoned, any threads that attempt to access this cell (via a dereference
or via an explicit call to force()) will panic.
use std::cell::LazyCell;
let hello = "Hello, World!".to_string();
let lazy = LazyCell::new(|| hello.to_uppercase());
assert_eq!(&*lazy, "HELLO, WORLD!");
If the initialization closure panics (the one that is passed to the new() method), the
panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
accesses of the cell (via force() or a dereference) to panic.
If the initialization closure panics (the one that is passed to the new() method), the
panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
accesses of the cell (via force() or a dereference) to panic.
use std::cell::LazyCell;
let mut lazy = LazyCell::new(|| 92);
let p = LazyCell::force_mut(&mut lazy);
assert_eq!(*p, 92);
*p = 44;
assert_eq!(*lazy, 44);
use std::cell::LazyCell;
let lazy = LazyCell::new(|| 92);
assert_eq!(LazyCell::get(&lazy), None);
let _ = LazyCell::force(&lazy);
assert_eq!(LazyCell::get(&lazy), Some(&92));
If the initialization closure panics (the one that is passed to the new() method), the
panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
accesses of the cell (via force() or a dereference) to panic.
If the initialization closure panics (the one that is passed to the new() method), the
panic is propagated to the caller, and the cell becomes poisoned. This will cause all future
accesses of the cell (via force() or a dereference) to panic.