More container types support trait objects

Minimum Rust version: 1.2

In Rust 1.0, only certain, special types could be used to create traitobjects.

With Rust 1.2, that restriction was lifted, and more types became able to do this. For example,Rc<T>, one of Rust's reference-counted types:

  1. use std::rc::Rc;
  2. trait Foo {}
  3. impl Foo for i32 {
  4. }
  5. fn main() {
  6. let obj: Rc<dyn Foo> = Rc::new(5);
  7. }

This code would not work with Rust 1.0, but now works.

If you haven't seen the dyn syntax before, see the section onit. For versions that do not support it, replace Rc<dyn Foo>with Rc<Foo>.