Links: Fully Qualified Syntax for Disambiguation: Calling Methods with the Same Name

In Rust, Fully Qualified Syntax (FQS) is used for disambiguation when calling methods with the same name from different traits on the same type12. This situation can arise when multiple traits with methods of the same name are implemented for a type, and Rust needs to know exactly which method you’re referring to.

Here’s the general form of FQS:

<Type as Trait>::function(receiver_if_method, other_args);

Let’s look at an example. Suppose you have two traits, TraitA and TraitB, both with a method named foo. You have a struct Bar that implements both traits:

trait TraitA {
    fn foo(&self);
}

trait TraitB {
    fn foo(&self);
}

struct Bar;

impl TraitA for Bar {
    fn foo(&self) {
        println!("TraitA's foo");
    }
}

impl TraitB for Bar {
    fn foo(&self) {
        println!("TraitB's foo");
    }
}

If you want to call foo from TraitA for an instance of Bar, you would use FQS like this:

<Bar as TraitA>::foo(&bar_instance);

And if you want to call foo from TraitB, you would use:

<Bar as TraitB>::foo(&bar_instance);