expression
Retry fallible async initialization
A failed initialization does not cache a successful value. A later caller may retry. Set an explicit retry and failure policy for costly external initialization, rather than assuming this type caches errors. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true 12 Common mistake: Assuming the first initialization error permanently prevents a retry.
Formal Syntax & Structure
rust syntaxrust
cell.get_or_try_init(|| async { Result::<T, E> }).awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { let cell = tokio::sync::OnceCell::<u32>::new(); let failed = cell.get_or_try_init(|| async { Err::<u32, _>("missing") }).await; println!("{}", failed.is_err()); let v = cell.get_or_try_init(|| async { Ok::<u32, &str>(12) }).await.unwrap(); println!("{v}");}