title: How to get a reference to a concrete type from a trait object?
body: <p>How do I get <code>Box&lt;B&gt;</code> or <code>&amp;B</code> or <code>&amp;Box&lt;B&gt;</code> from the <code>a</code> variable in this code:</p>
  
  <pre><code>trait A {}
  
  struct B;
  impl A for B {}
  
  fn main() {
      let mut a: Box&lt;dyn A&gt; = Box::new(B);
      let b = a as Box&lt;B&gt;;
  }
  </code></pre>
  
  <p>This code returns an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0605]: non-primitive cast: `std::boxed::Box&lt;dyn A&gt;` as `std::boxed::Box&lt;B&gt;`
   --&gt; src/main.rs:8:13
    |
  8 |     let b = a as Box&lt;B&gt;;
    |             ^^^^^^^^^^^
    |
    = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
  </code></pre>