简体   繁体   中英

What does the exclamation point mean in a trait implementation?

I found in the library reference for std::rc::Rc this trait implementation

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

What does the exclamation point in front of Send mean?

I consulted both The Rust Programming Language ¹ book and The Rust Reference ², but didn't find an explanation. Please give a reference in your answer.


¹ especially the [section3.19 Traits
² and sections 5.1 Traits and 5.1 Implementations

It's a negative trait implementation for the Send trait as described in RFC 19 .

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:

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.)

An auto trait may not define any methods, which also makes it a marker 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:

impl !Send for MyType {}

This means that even though MyType only contains other types that are Send , MyType itself will not automatically implement Send .

See also the answer to another question: What is an auto trait in Rust?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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