Próbując skompilować follwing kod antykorozyjneZastosowanie cecha z modułem z tej samej nazwie co struct
mod traits {
pub trait Dog {
fn bark(&self) { println!("Bow"); }
}
}
struct Dog;
impl traits::Dog for Dog {}
fn main() {
let dog = Dog;
dog.bark();
}
daje komunikat o błędzie
<anon>:13:9: 13:15 error: type `Dog` does not implement any method in scope named `bark`
<anon>:13 dog.bark();
^~~~~~
<anon>:13:9: 13:15 help: methods from traits can only be called if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
<anon>:13:9: 13:15 help: candidate #1: use `traits::Dog`
Jeśli dodać use traits::Dog;
, błąd staje:
<anon>:1:5: 1:16 error: import `Dog` conflicts with type in this module [E0256]
<anon>:1 use traits::Dog;
^~~~~~~~~~~
<anon>:9:1: 9:12 note: note conflicting type here
<anon>:9 struct Dog;
^~~~~~~~~~~
Po zmianie nazwy trait Dog
na trait DogTrait
wszystko działa. Ale jak mogę użyć cechy z modułu częściowego, który ma taką samą nazwę jak struktura w moim głównym module?