简体   繁体   English

感叹号在 trait 实现中是什么意思?

[英]What does the exclamation point mean in a trait implementation?

I found in the library reference for std::rc::Rc this trait implementation我在std::rc::Rc的库参考中找到了这个特性实现

impl<T> !Send for Rc<T> 
where
    T: ?Sized, 

What does the exclamation point in front of Send mean? Send前面的感叹号是什么意思?

I consulted both The Rust Programming Language ¹ book and The Rust Reference ², but didn't find an explanation.我查阅了The Rust Programming Language ¹ book 和The Rust Reference ²,但没有找到解释。 Please give a reference in your answer.请在您的回答中提供参考。


¹ especially the [section3.19 Traits ¹尤其是 [第3.19特征
² and sections 5.1 Traits and 5.1 Implementations ²5.1 特性5.1 实现部分

It's a negative trait implementation for the Send trait as described in RFC 19 .它是RFC 19 中描述的Send trait 的负面 trait 实现

As a summary: The Send trait is an auto trait , which means it is automatically implemented for all types that only contain other Send types:总结: Send trait 是一个auto trait ,这意味着它会自动为所有只包含其他Send类型的类型实现:

unsafe auto trait Send {}

( Send is also an unsafe trait , which means it is unsafe to implement, but that is not relevant to the question.) Send也是一个不安全的 trait ,这意味着实现它是不安全的,但这与问题无关。)

An auto trait may not define any methods, which also makes it a marker trait .一个auto trait 可能没有定义任何方法,这也使它成为一个标记 trait (The syntax for defining auto traits is currently only available in the standard library or on the nightly compiler, but their semantics are stable.) (定义自动特征的语法目前仅在标准库或夜间编译器中可用,但它们的语义是稳定的。)

To opt out of the automatic implementation of Send , you must write an explicit negative trait implementation:要选择退出Send的自动实现,您必须编写一个明确的否定 trait 实现:

impl !Send for MyType {}

This means that even though MyType only contains other types that are Send , MyType itself will not automatically implement Send .这意味着即使MyType只包含Send其他类型, MyType本身也不会自动实现Send

See also the answer to another question: What is an auto trait in Rust?另请参阅另一个问题的答案: Rust 中的 auto trait 是什么?

这是一个负面特征 impl ,因此您可以将其理解为选择退出Send特征。

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

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