More container types support trait objects
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:
use std::rc::Rc;
trait Foo {}
impl Foo for i32 {
}
fn main() {
let obj: Rc<dyn Foo> = Rc::new(5);
}
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, replaceRc<dyn Foo>
withRc<Foo>
.