title: How can I downcast a non-'static object?
body: <pre><code>use std::any::Any;
  
  trait Animal {
      fn as_any(&amp;self) -&gt; &amp;dyn Any;
  }
  
  struct Dog { }
  impl Dog {
      fn yell(&amp;self) {
          println!(&quot;Wouaf !&quot;)
      }
  }
  impl Animal for Dog {
      fn as_any(&amp;self) -&gt; &amp;dyn Any {
          self
      }
  }
  fn main() {
      let dog = Dog { };
      let mut animals = Vec::new() as Vec&lt;&amp;dyn Animal&gt;;
      animals.push(&amp;dog);
      let animal = animals.get_mut(0).unwrap();
      animal.as_any().downcast_ref::&lt;Dog&gt;().unwrap().yell();
  }
  </code></pre>
  <p>This code compiles great. From what I've read, <code>as_any()</code> is a good way to downcast.</p>
  <p>Now I add a field <code>Collar</code> to my struct <code>Dog</code> which will be a reference. I add a lifetime on my struct <code>Dog</code> to prevent dangling references with <code>Collar</code>.</p>
  <pre><code>use std::any::Any;
  
  ...
  struct Collar {
      name: String
  }
  struct Dog&lt;'a&gt; {
      collar: &amp;'a Collar
  }
  impl &lt;'a&gt; Dog&lt;'a&gt; {
      fn yell(&amp;self) {
          println!(&quot;{}: Wouaf !&quot;, self.collar.name)
      }
  }
  fn main() {
      let collar = Collar { name: String::from(&quot;Snoopy !!&quot;) };
      ...
  }
  </code></pre>
  <p>The code does not compile anymore. From what I understand <code>Any</code> come with static lifetime, but I have defined a <code>'a</code> lifetime which is incompatible. In this case, how can I solve the issue in order to achieve the downcast of <code>Animal</code> to <code>Dog</code>?</p>