What does this syntax mean (Sum trait, A = Self)

I couldn't find documentation for this anywhere:

pub trait Sum<A = Self> { //A = Self?
    fn sum<I>(iter: I) -> Self where I: Iterator<Item=A>;
}

What purpose does the 'A = Self' serve? It doesn't look like a trait bound or anything...

It's a default type parameter. Check out the Add trait for ex:

pub trait Add<RHS = Self> {
    type Output;
    fn add(self, rhs: RHS) -> Self::Output;
}

If, for example, you wanted to implement it for a type Foo, you can forgo mentioning the type parameter and write:

impl Add for Foo { ... }

instead of

impl Add<Foo> for Foo { ... }

It makes the syntax a little nicer, makes things more simple for common use cases, and also allows to add genericity to a trait or a struct in a backward compatible way.

See the RFC and the tracking issue if you want more details.

2 Likes

Ahhh no kidding -- makes sense to me. Especially the backward compatible part.

Thanks man!