Static Trait Methods
It can’t. A trait can only describe static methods on implementations of the trait. Traits do not have methods; traits have method signatures and can optionally provide “suggested implementations”. To quote myself:
To further illustrate this point, the following doesn’t work:
trait Test {
fn print() {
println!("test");
}
}
fn main() {
Test::print();
}
Why? Because Test describes an interface and doesn’t really exist. If you want this to work, you have to add:
impl Test for () {}
As a matter of fact, you don’t even have to implement it on (), it just has to be implemented on something:
struct Stuff;
impl Test for Stuff {}
TL;DR, traits don’t have methods; they have method signatures and default implementations.
The method exists on the data structure. Test::print is just the name of the method (see below).
Method Naming
Trait::method is absolutely necessary because it’s the fully qualified name of the method and rustc can’t always figure it out.
Given:
trait TraitA {
fn do_it(&self) {
println!("TraitA")
}
}
trait TraitB {
fn do_it(&self) {
println!("TraitA")
}
}
struct Data;
impl TraitA for Data {}
impl TraitB for Data {}
The following obviously doesn’t work because rust doesn’t know which do_it method to call:
fn main() {
let d = Data;
d.do_it();
}
In this case, you have to use the fully qualified function name:
fn main() {
let d = Data;
TraitA::do_it(&d);
}