Downcast to trait

Suppose I've TabBar → Control → DisplayObject.

This line doesn't work:

println!("{}", Rc::downcast::<dyn Control>(o).unwrap().x()); // 64

I get:

error[E0277]: the size for values of type `dyn Control` cannot be known at compilation time
    --> src/main.rs:26:20
     |
26   |     println!("{}", Rc::downcast::<dyn Control>(o).unwrap().x()); // 64
     |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
     |

This is just a “problem” description. If you need support with your code, or help to improve your understanding of the Rust language (including questions like the inner workings of trait objects, downcasting, or why certain “limitations” exist), users.rust-lang.org might be a better place to ask.

3 Likes