October 2018
Beginner
180 pages
4h 48m
English
When a variable's type is Box<dyn Any>, it acts much like an &dyn Any, but gains a new feature. A normal &dyn Any has a downcast_ref function that we can use to get a reference to the contained value, if we know what type to use to extract it. Now, &dyn mut Any adds a downcast_mut that we can use to get a mutable reference. When we have a Box<dyn Any>, we have access to both of those functions, but we can also call a plain downcast function to move the contained value out of the Any and into a variable of the correct type. This consumes the Box and the Any, and gives us back a new Box containing the data value with its correct data type.
Read now
Unlock full access