Reference to a static variable inside a struct without a lifetime
⚓ Rust 📅 2026-03-02 👤 surdeus 👁️ 2Hi there,
I'd like to have code like this:
use std::sync::LazyLock;
static FOO: LazyLock<Vec<u32>> = LazyLock::new(|| vec![1, 2, 5]);
struct Struct {
foo_ref: &u32,
// ..any other struct fields
}
Where foo_ref is specifically a reference to FOO. As written, this code requires a lifetime specifier, and I was hoping there was a way to avoid needing to include that by somehow specifying that it's a reference to a static variable (which from my understanding should mean that there is no scenario where Struct would outlive FOO). Since the relevant struct is used in several different places in my code, mainly as a field in other structs, I would prefer to not have to add lifetime specifiers to all of them if possible - especially since this seems more a problem with my code or my design than a genuine need for adding specifiers.
Is there a way to include such a reference in a struct, or another way to go about doing a similar thing?
3 posts - 2 participants
🏷️ Rust_feed