title: What are the problems of using Any trait objects in heterogeneous Rust collections?
body: <p>In dynamic languages, like Clojure, it is easy to express collections with different types:</p>
  
  <pre class="lang-clj prettyprint-override"><code>{:key1 "foo", :key2 [34 "bar" 4.5], "key3" {:key4 "foobar"}}
  </code></pre>
  
  <p>In Rust, the preferred way to implement such collections is <a href="https://stackoverflow.com/questions/27957103/how-do-i-create-a-heterogeneous-collection-of-objects">using trait objects or enums</a>. The use of the <code>Any</code> trait object seems to be the most flexible approach (if there isn't a fixed number of known type alternatives) as it allows downcasting to the actual object types:  </p>
  
  <pre><code>let mut vector: Vec&lt;Box&lt;Any&gt;&gt; = Vec::new();
  vector.push(Box::new("I’m"));
  vector.push(Box::new(4 as u32));
  console!(log, vector[0].downcast_ref::&lt;&amp;str&gt;());
  console!(log, vector[1].downcast_ref::&lt;u32&gt;());
  </code></pre>
  
  <p>This approach seems to be discouraged. What are its disadvantages? </p>