Polymorphism fn

Hi Dear Community,

I got trait Marker and two structs that impls Marker.

trait Marker: Sized {
    fn test() -> String;
}

I wanna make sth like:

fn get::<T>() 
where T:Marker
{
    T::test()
}

fn get::<T, B>()
where T:Martker,
B:Marker
{
    T::test();
    B::test();
}

but it seems rust doesn't allow that.
is it possible way to make it accept any number of uninstantiated structs?
I cannot use macro since this fn needs to be attached to another struct.

Remove the double colon. You need that only when calling a function, and not when defining it.

trait Marker: Sized {
    fn test() -> String;
}

fn get<T>()
where
    T: Marker,
{
    T::test();
}

fn get2<T, B>()
where
    T: Marker,
    B: Marker,
{
    T::test();
    B::test();
}
4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.