1

In dynamic languages, like Clojure, it is easy to express collections with different types:

{:key1 "foo", :key2 [34 "bar" 4.5], "key3" {:key4 "foobar"}}

In Rust, I have seen the use of enums:

pub enum Value {
    Null,
    Bool(bool),
    Number(f64),
    String(String),
    ...
}

But different programmers will choose different enums to represent the same encapsulated type. There is also the Any trait but it is experimental.

What is the idiomatic way in Rust to represent collections of items of different types? Some examples and/or links to implementations would be very nice.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dilvan
  • 2,109
  • 2
  • 20
  • 32
  • 3
    Note that Rust likes types very much! – nbro Jul 05 '18 at 21:17
  • 2
    `Any` is not experimental… – Boiethios Jul 05 '18 at 21:17
  • 1
    Only the [`get_type_id`](https://doc.rust-lang.org/std/any/trait.Any.html#tymethod.get_type_id) method is experimental. But the trait is generally to be avoided any ways. Just use an `enum`. – mcarton Jul 05 '18 at 21:25
  • Are these 2 implementations (traits and enums) the idiomatic choices in Rust? Do you have links to examples of Any used with collections in Rust? – dilvan Jul 06 '18 at 03:35

0 Answers0