如何创建静态可变哈希图? [英] How to create a static mutable hashmap?

查看:63
本文介绍了如何创建静态可变哈希图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建静态可变哈希图?我可以接受不安全的代码.

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

普通静态不允许带有构造函数的全局变量.

The normal static does not allow globals with constructors.

举个例子,我想要https://gist.github.com/Kimundi/8782487 但 HASHMAP 是可变的.

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.

推荐答案

有关维护的答案,请参阅如何创建一个全局的、可变的单例?,因为这个问题应该被标记为重复.

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

鉴于您已经为非可变的全局对象提供了解决方案,也许您可​​以使用 cell 容器添加内部可变性?

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?

实际上,这听起来是个坏主意.全局可变状态是有问题的.为什么不能将可变 hashmap 传递给需要它的方法/对象?

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?

您还可以查看什么 stdin 的实现,提供对真正全局单例的安全访问.

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

这篇关于如何创建静态可变哈希图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆