title: Is there a way to init a non-trivial static std::collections::HashMap without making it static mut?
body: <p>In this code, <code>A</code> does not need to be <code>static mut</code>, but the compiler forces <code>B</code> to be <code>static mut</code>:</p>
  
  <pre><code>use std::collections::HashMap;
  use std::iter::FromIterator;
  
  static A: [u32; 21] = [
      0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  ];
  static mut B: Option&lt;HashMap&lt;u32, String&gt;&gt; = None;
  
  fn init_tables() {
      let hm = HashMap::&lt;u32, String&gt;::from_iter(A.iter().map(|&amp;i| (i, (i + 10u32).to_string())));
      unsafe {
          B = Some(hm);
      }
  }
  
  fn main() {
      init_tables();
      println!("{:?} len: {}", A, A.len());
      unsafe {
          println!("{:?}", B);
      }
  }
  </code></pre>
  
  <p>This is the only way I have found to get close to what I actually want:  a global, immutable <code>HashMap</code> to be used by several functions, without littering all my code with <code>unsafe</code> blocks.</p>
  
  <p>I know that a global variable is a bad idea for multi-threaded applications, but mine is single threaded, so why should I pay the price for an eventuality which will never arise?</p>
  
  <p>Since I use <code>rustc</code> directly and not <code>cargo</code>, I don't want the "help" of extern crates like <code>lazy_static</code>. I tried to decypher what the macro in that package does, but to no end.</p>
  
  <p>I also tried to write this with <code>thread_local()</code> and a <code>RefCell</code> but I had trouble using <code>A</code> to initialize <code>B</code> with that version.</p>
  
  <p>In more general terms, the question could be "How to get stuff into the initvars section of a program in Rust?"</p>
  
  <p>If you can show me how to initialize <code>B</code> directly (without a function like <code>init_tables()</code>), your answer is probably right.  </p>
  
  <p>If a function like <code>init_tables()</code> is inevitable, is there a trick like an accessor function to reduce the <code>unsafe</code> litter in my program?</p>