0
trait Component {}

struct UserInput {}
struct Sidebar {}

impl Component for UserInput {}
impl Component for Sidebar {}

struct App {
    search_input: UserInput,
    sidebar: Sidebar,
}

impl App {
    fn new() -> Self {
        Self {
            search_input: UserInput {},
            sidebar: Sidebar {},
        }
    }
    
    fn get_widgets(&self) -> Vec<&mut dyn Component> {
        vec![
            &mut self.search_input,
            &mut self.sidebar,
        ]
    }
}

fn main() {
    let mut app = App::new();

    let widget_index = app.get_widgets().iter().position(|&w| w == app.search_input).unwrap();
}

The w returns type &mut dyn Component which can't be comparable with app.search_input (type UserInput). So how can I do it, or is any dynamic casting needed?

Here is the error:

error[E0369]: binary operation `==` cannot be applied to type `&mut dyn Component`
      --> src/lib.rs:53:26
       |
    53 |         .position(|&w| w == app.search_input)
       |                        - ^^ ---------------- UserInput
       |                        |
       |                        &mut dyn Component
Thong Nguyen
  • 143
  • 3
  • 10

0 Answers0