You need to specify a specific implementation of the trait in order to call the
method.
Erroneous code example:
#![allow(unused)]fnmain() {
traitCoroutine {
fncreate() -> u32;
}
structImpl;
impl Coroutine for Impl {
fncreate() -> u32 { 1 }
}
structAnotherImpl;
impl Coroutine for AnotherImpl {
fncreate() -> u32 { 2 }
}
let cont: u32 = Coroutine::create();
// error, impossible to choose one of Coroutine trait implementation// Should it be Impl or AnotherImpl, maybe something else?}
ⓘ
This error can be solved by adding type annotations that provide the missing
information to the compiler. In this case, the solution is to use a concrete
type:
#![allow(unused)]fnmain() {
traitCoroutine {
fncreate() -> u32;
}
structAnotherImpl;
impl Coroutine for AnotherImpl {
fncreate() -> u32 { 2 }
}
let gen1 = AnotherImpl::create();
// if there are multiple methods with same name (different traits)let gen2 = <AnotherImpl as Coroutine>::create();
}