Return Self in trait

Hello,

If I have a trait T and I want to define a function that returns Self for any type that implement T. Something like that

trait T {
    fn f(&self) -> ?????;
}

struct S {
    ...
}

impl T for S {
    fn f(&self) -> "I want to return S" {
        // Do Something
        S { ... }
    }
}

struct R {
    ...
}

impl T for R {
    fn f(&self) -> "I want to return R" {
        // Do Something
        R { ... }
    }
}

I know I could add type ReturnSelf to the trait and always set type ReturnSelf = Self, but it is a bit cumbersome.

1 Like

I think you can do just -> Self in trait as long as it is Sized

3 Likes

Thanks, it seems to work.