What does equal sign mean in a trait bound?

Viewed 410

For example in raw_vec.rs:

pub struct RawVec<T, A: AllocRef = Global> {
    ptr: Unique<T>,
    cap: usize,
    alloc: A,
}

I can see Global is a struct that implements AllocRef trait.

I'm surprised I can't find any explanation in rust books. I appreciate any links to docs on the topic.

If I had to guess it's a way to further constrain the impls that 'A' can take, but then why need 'A' generic type at all? It seems simpler to define the field alloc as Global. Again this is just a guess. Clarification appreciated.

Thanks!

1 Answers

It's a default generic type. It will be used unless you explicitly specify another in the concrete implementation. It's mentioned in the rust book:

When we use generic type parameters, we can specify a default concrete type for the generic type. This eliminates the need for implementors of the trait to specify a concrete type if the default type works. The syntax for specifying a default type for a generic type is <PlaceholderType=ConcreteType> when declaring the generic type.

Related