I cannot set value for OnceCell

⚓ Rust    📅 2025-11-27    👤 surdeus    👁️ 7      

surdeus

I have tried both std::cell::oncecell and smol::lock::OnceCell. Same result.

First, I have pub const INGRESS_KIND: OnceCell<GroupVersionKind> = OnceCell::new(); in a module a. GroupVersionKind is a struct with three String members.

Then in main.rs, in async main function, I have this at the beginning.

let y = GroupVersionKind {
        group: "networking.k8s.io".to_owned(),
        version: "v1".to_owned(),
        kind: "Ingress".to_owned(),
    };
    match INGRESS_KIND.set(y).await {
        Ok(_) => tracing::debug!("{:?}", INGRESS_KIND.get().unwrap()),
        Err(_) => panic!("Cannot init INGRESS_KIND"),
    }

The set always returns Ok, std version or smol version. But INGRESS_KIND.get() always returns None.

What did I do wrong?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed