Got the problem to run example in "ch17-02-trait-objects"

Hi there, I'm following the official book tutorial to go through all Rust concept, but I'm stopped on the topic of

Using Trait Objects That Allow for Values of Different Types

I put all the code together below and try to run and have a look:

pub trait Draw {
    fn draw(&self);
}

pub struct Screen<T: Draw> {
    pub components: Vec<T>,
}

impl<T> Screen<T>
where
    T: Draw,
{
    pub fn run(&self) {
        for component in self.components.iter() {
            component.draw();
        }
    }
}

pub struct Button {
    pub width: u32,
    pub height: u32,
    pub label: String,
}

impl Draw for Button {
    fn draw(&self) {
        // code to actually draw a button
    }
}

fn main() {
    struct SelectBox {
        width: u32,
        height: u32,
        options: Vec<String>,
    }

    impl Draw for SelectBox {
        fn draw(&self) {
            // code to actually draw a select box
        }
    }

    let screen = Screen {
        components: vec![
            Box::new(SelectBox {
                width: 75,
                height: 10,
                options: vec![
                    String::from("Yes"),
                    String::from("Maybe"),
                    String::from("No"),
                ],
            }),
            Box::new(Button {
                width: 50,
                height: 10,
                label: String::from("OK"),
            }),
        ],
    };

    screen.run();
}

But I can't compile it, and the error is quite confusing me, as I thought the official example should compile and run:

   --> src/main.rs:210:22
    |
210 |               Box::new(Button {
    |  ______________________^
211 | |                 width: 50,
212 | |                 height: 10,
213 | |                 label: String::from("OK"),
214 | |             }),
    | |_____________^ expected struct `main::SelectBox`, found struct `Button`

error[E0277]: the trait bound `std::boxed::Box<main::SelectBox>: Draw` is not satisfied
   --> src/main.rs:199:18
    |
159 | pub struct Screen<T: Draw> {
    | -------------------------- required by `Screen`
...
199 |     let screen = Screen {
    |                  ^^^^^^ the trait `Draw` is not implemented for `std::boxed::Box<main::SelectB
ox>`

error[E0599]: no method named `run` found for struct `Screen<std::boxed::Box<main::SelectBox>>` in t
he current scope
   --> src/main.rs:218:12
    |
159 | pub struct Screen<T: Draw> {
    | -------------------------- method `run` not found for this
...
218 |     screen.run();
    |            ^^^ method not found in `Screen<std::boxed::Box<main::SelectBox>>`
    |
    = note: the method `run` exists but the following trait bounds were not satisfied:
            `std::boxed::Box<main::SelectBox> : Draw`

error: aborting due to 3 previous errors

How to solve it actually? plz :slight_smile:

This forum is for the development of Rust (the language, the compiler, and its associated tools), not for using Rust. The Users forum is the better place to ask this question.

2 Likes

Thanks :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.