How to handle "multiple applicable items in scope" error?

Viewed 1690

I'm using the fltk-rs crate and running into the "multiple applicable items in scope" error.

fltk = "0.10.14"
use fltk::{table::*};

pub struct AssetViewer {
    pub table: Table, 
}

impl AssetViewer {
    pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
        let mut av = AssetViewer {
            table: Table::new(x,y,width-50,height,""),

        };
        av.table.set_rows(5);
        av.table.set_cols(5);
        av
    }
    pub fn load_file_images(&mut self, asset_paths: Vec<String>){
        self.table.clear(); //<- throws multiple applicable items in scope
    }
}

Gives error:

error[E0034]: multiple applicable items in scope
   --> src\main.rs:18:20
    |
18  |         self.table.clear(); //<- throws multiple applicable items in scope
    |                    ^^^^^ multiple `clear` found
    |
    = note: candidate #1 is defined in an impl of the trait `fltk::TableExt` for the type `fltk::table::Table`
    = note: candidate #2 is defined in an impl of the trait `fltk::GroupExt` for the type `fltk::table::Table`

I would like to specify that I'm referencing the TableExt trait, not the GroupExt trait. How would I do this?

2 Answers

TLDR: Use fully qualified function name:

fltk::GroupExt::clear(&mut self.table)

Consider this simplified example:

struct Bar;

trait Foo1 {
    fn foo(&self) {}
}
trait Foo2 {
    fn foo(&self) {}
}
impl Foo1 for Bar {}
impl Foo2 for Bar {}

fn main() {
    let a = Bar;
    a.foo()
}

It will fail to compile with the following error message:

error[E0034]: multiple applicable items in scope
  --> src/main.rs:16:7
   |
16 |     a.foo()
   |       ^^^ multiple `foo` found
   |

Compiler will also suggest a solution:

help: disambiguate the associated function for candidate #1
   |
16 |     Foo1::foo(&a)
   |

The compiler in this instance recommends the correct fix. The error message (as of 1.48.0) contains these help annotations:

help: disambiguate the associated function for candidate #1
    |
18  |         fltk::TableExt::clear(&mut self.table);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: disambiguate the associated function for candidate #2
    |
18  |         fltk::GroupExt::clear(&mut self.table);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The full error message actually shows 5 candidates and 5 help annotations.


As an aside, another syntax for disambiguating methods is:

<Table as fltk::TableExt>::clear(&mut self.table);

Though its more verbose than the recommended syntax and its only needed in situations where the method doesn't take self and therefore can't deduce Table.

Related