Given the following trait:
trait Trait { type Associated; }
and an impelmentation:
struct Struct {}
impl Trait for Struct { type Associated = u32; }
I would like to retrieve the associated type Associated
of Struct
.
Somehting like (Struct as Trait
is invalid rust):
struct AnotherStruct {
prop: (Struct as Trait)::Associated
}
Is there any way to retrieve the asscoiated type?
Note that I am aware that I can impelment a function like:
fn f<T: Trait>(arg: T::Associated) -> T::Associated { arg }
However, I would like to get the associaetd type to set the type of a struct property.
You were so close. The correct fully qualified syntax is <Type as Trait>::AssociatedType
. You can read more about it in The Book.
Your example should look like this.
trait Trait {
type Associated;
}
struct Struct {}
impl Trait for Struct {
type Associated = u32;
}
struct AnotherStruct {
prop: <Struct as Trait>::Associated
}