title: How to insert a non-static value to a static HashMap with &'static dyn Any values?
body: <p>I am trying to create a static <code>HashMap</code> that I will read from or write to throughout the program.</p>
  <p>Here's my attempt (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=30113e840e65c3ef4b1efa62d03a9b38" rel="nofollow noreferrer">playground</a>):</p>
  <pre class="lang-rust prettyprint-override"><code>extern crate lazy_static; // 1.4.0
  
  use std::{any::Any, collections::HashMap};
  
  use chrono::{Local, NaiveDate}; // 0.4.19
  
  fn test() -&gt; () {
      let cache: &amp;'static mut HashMap&lt;String, HashMap&lt;String, &amp;'static dyn Any&gt;&gt; =
          &amp;mut HashMap::new();
  
      let function_name = &quot;Zaxd&quot;.to_string();
      let params = vec![&quot;zuha&quot;.to_string(), &quot;haha&quot;.to_string(), &quot;hahaha&quot;.to_string()];
      let date = Local::today().naive_local();
  
      write(function_name, params, &amp;date, &amp;18, cache);
  }
  
  fn write(
      function_name: String,
      params: Vec&lt;String&gt;,
      valid_to: &amp;'static (dyn Any + 'static),
      return_value: &amp;'static (dyn Any + 'static),
      cache: &amp;'static mut HashMap&lt;String, HashMap&lt;String, &amp;'static dyn Any&gt;&gt;,
  ) -&gt; () {
      let mut key = function_name;
  
      key.push_str(&quot;-&quot;);
      key.push_str(&amp;params.join(&quot;-&quot;));
  
      let mut value: HashMap&lt;String, &amp;dyn Any&gt; = HashMap::new();
  
      value.insert(&quot;value&quot;.to_string(), return_value);
      value.insert(&quot;valid_to&quot;.to_string(), valid_to);
  
      cache.insert(key.clone(), value);
  }
  
  fn main() {
      test();
  }
  </code></pre>
  <p>I am getting the following errors:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0716]: temporary value dropped while borrowed
    --&gt; src/main.rs:9:14
     |
  8  |     let cache: &amp;'static mut HashMap&lt;String, HashMap&lt;String, &amp;'static dyn Any&gt;&gt; =
     |                --------------------------------------------------------------- type annotation requires that borrow lasts for `'static`
  9  |         &amp;mut HashMap::new();
     |              ^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
  ...
  16 | }
     | - temporary value is freed at the end of this statement
  
  error[E0597]: `date` does not live long enough
    --&gt; src/main.rs:15:34
     |
  15 |     write(function_name, params, &amp;date, &amp;18, cache);
     |                                  ^^^^^
     |                                  |
     |                                  borrowed value does not live long enough
     |                                  cast requires that `date` is borrowed for `'static`
  16 | }
     | - `date` dropped here while still borrowed
  </code></pre>
  <p>I have two questions:</p>
  <ol>
  <li>How do I solve this error?</li>
  <li>I want the hashmap to be static but not the values I insert into it (i.e. I want the values to be in the hashmap but that variable should be deleted). But with this implementation, it looks like they will in the heap as long as the program runs which is unacceptable for my purposes. However, I cannot define them to live less than static because then the compiler complains about how they need to live at least as long as static. What is the correct implementation for a static mutable hashmap?</li>
  </ol>