How to check value from all parts of code

I need function to check some values from async functions of projects. I wanted to make const hashset and check values:


but rust suppose not to use const global variables and i get error trying to make one (non const function for const variable). I inclined to think there are possible ways to do it without allocating map every time function check was called, but i have not enough knowledge to implement this.

To avoid a global, consider creating an Arc<HashSet<u64>> if it is immutable, or Arc<RwLock<HashSet<u64>>> if it must be mutable. And pass this to where it is needed.

You can call Arc::clone if you need multiple "handles" in different threads. If it is only accessed by one thread, but you still need multiple "handles", use Rc instead of Arc. Cloning Arc or Rc will just increment its reference count, it will not copy/clone the HashSet.

If you must use a global, use a static variable and initialize it with OnceLock. I don't think you can create a const HashSet, just because there are probably none currently implemented.

1 Like

Every time you use a const value, it's as if you defined it in place. So even if it was possible, you'd want to put it in a static.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.