全局不可变哈希集

mot*_*son 6 rust

如何创建一个不可变的、全局可访问的 HashSet 来存储编译时已知的值?我知道全局状态是不需要的,但由于它是不可变的,所以它可能仍然是可以接受的,对吧?

static global_set: HashSet<String> = create_set();

fn create_set() -> HashSet<String> {
    let mut new_set: HashSet<String> = HashSet::new();

    new_set.insert("ONE".to_string());
    new_set.insert("TWO".to_string());
    new_set.insert("THREE".to_string());

    new_set
}
Run Code Online (Sandbox Code Playgroud)

这是行不通的。

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
  --> src\lib.rs:62:38
   |
62 | static cclass_map: HashSet<String> = populate_hashmap();
   |                                      ^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

如果我做了create_seta const fn 它会抱怨the mutable references in const fn are unstable

我觉得必须有更好的方法来做到这一点。如何才能以更好的方式做到这一点?或者我怎样才能让它发挥作用?