1

I would like to know the ways to have a container containing several types. I know that:

  • A tuple can contain several types
  • If I create an enumeration E, I can create a Vec<E>.

In C++, we can create a Vec<A*> containing both B* and C* elements if B and C inherit from A. Can we do something similar in Rust? For instance, if several types implement the same trait?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
x4rkz
  • 513
  • 4
  • 19

1 Answers1

4

1) You can store references or pointers to trait objects.

2) You can create an enum over the things you want to store.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157