Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a static mutable hashmap? [closed]

Tags:

rust

How do I create a static mutable hashmap? I am ok with unsafe code.

The normal static does not allow globals with constructors.

As an example, I want what is at https://gist.github.com/Kimundi/8782487 but HASHMAP to be mutable.

I understand that global shared mutable state is not what very rust-ish but I just want to know if such a thing is possible.

like image 802
Jack Avatar asked Jan 02 '15 19:01

Jack


1 Answers

For maintained answers, see How do I create a global, mutable singleton?, as this question should have been marked as a duplicate.


Seeing as how you already have a solution for a global object that is non-mutable, perhaps you can use one of the cell containers to add interior mutability?

Realistically, this sounds like a a bad idea. Global mutable state is problematic. Why can't you pass in a mutable hashmap to the methods / objects that need it?

You may also what to check out the implementation of stdin, which provides safe access to a true global singleton.

like image 116
Shepmaster Avatar answered Nov 15 '22 05:11

Shepmaster