简体   繁体   English

等号在特征绑定中意味着什么?

[英]What does equal sign mean in a trait bound?

For example in raw_vec.rs:例如在 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.我可以看到 Global 是一个实现 AllocRef 特征的结构。

I'm surprised I can't find any explanation in rust books.我很惊讶在 rust 书籍中找不到任何解释。 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?如果我不得不猜测这是一种进一步限制'A'可以采用的impls的方法,那么为什么需要'A'泛型类型呢? It seems simpler to define the field alloc as Global .将字段alloc定义为Global似乎更简单。 Again this is just a guess.这只是一个猜测。 Clarification appreciated.澄清赞赏。

Thanks!谢谢!

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 :它在rust 书中提到:

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.如果默认类型有效,这消除了 trait 实现者指定具体类型的需要。 The syntax for specifying a default type for a generic type is <PlaceholderType=ConcreteType> when declaring the generic type.声明泛型类型时,为泛型类型指定默认类型的语法是<PlaceholderType=ConcreteType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM