title: Rust: cast trait object to a different type
body: <p>Is there any way to cast a trait object (<code>dyn A</code>) to another type (<code>dyn B</code>) safely?</p>
  
  <pre class="lang-rust prettyprint-override"><code>trait A {}
  trait B: A {}
  impl &lt;T: A&gt; B for T {}
  
  fn cast(x: &amp;dyn A) -&gt; &amp;dyn B {
    x
  }
  </code></pre>
  
  <pre><code>error[E0308]: mismatched types
   --&gt; src/lib.rs:6:3
    |
  6 |   x
    |   ^ expected trait `B`, found trait `A`
    |
    = note: expected type `&amp;dyn B`
               found type `&amp;dyn A`
  </code></pre>