For example suppose that you are using one of the creational patterns for creating objects that have unique ID. The creational pattern should use a
static mut variable used to keep track of last assigned ID (that will be increased it in unsafe way), this variable will be declared somewhere outside the struct that actually is using and could be visible/accessible by other function.
In my point of view there are three problems with this approach:
- Unsafe code is needed to increment the variable.
- Data racing, since unsafe code is executed (for example I noticed this problem running some tests that I’ve prepared and I found out that
cargo testruns each test function in a separated thread by default)
- The struct depends on a variable that is not encapsulated in itself and it is accessible by all items of the same module.
I think that there should be a better solution than this one… Could you help me?