Program scope unique structs (mutable flavored)

2026-11-12来源 Rust Users ForumEnglish

Apparently I've been playing a game with myself to see how many times I can answer my own questions in the process of figuring how to phrase them correctly enough to be understandable. Regardless, I still haven't found a firm 'does this exist' in my efforts this time, so I thought it might be worth posting to ask.

The thought is that it would be convenient for my purposes to have a struct, say StateVector, that holds some arbitrary number of values or sub structures, that:

  1. There will only ever be one instance of StateVector in existence for the life of the program.
  2. The sole instance of StateVector is constructed at runtime, so any reference to it will either be None (if referenced before it is constructed) or a valid StateVector.
  3. The instance is mutable.
  4. The instance is program scope level.

Such that a function definition could reference the StateVector instance without requiring it as a parameter, something like (hand-waving error handling):

impl FakeStruct { fn check(&self) -> bool { return StateVector.method(&self) } } 

Const and static seemed to be the closest I could dig up in a short amount of time, but const looks to be compile-time determined, and static looks like it has issues with mutability. Or function definitions can't reference things not declared in their parameters, and I've wandered out into imaginary weeds.

2 posts - 2 participants

Read full topic

原文:https://www.a8q9.cn/en/article/program-scope-unique-structs-mutable-flavored-3b058a.html

相关文章