问题
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 [section 3.19 Traits
² and sections 5.1 Traits and 5.1 Implementations
回答1:
It's a negative trait implementation for the Send
trait as described in RFC 19.
As a summary: The Send
trait is an unsafe trait. The RFC says:
[It] is unsafe to implement, because implementing it carries semantic guarantees that, if compromised, threaten memory safety in a deep way.
However, it is implemented by default for all types. Currently the syntax is:
unsafe impl Send for .. { }
Note .. as the syntax for a default implementation. The trait must also not define any methods. A default implementation is a marker trait implemented by all types
There is new syntax on its way for default traits, and they will be called auto trait
, but as of now those are in experimental and possibly buggy. But here's is the syntax for future use:
unsafe auto trait Send { }
Therefore, to opt out of Send
, write a negative trait implementation. This syntax will stay the same, even once auto traits are stable:
impl !Send for MyType { }
See also the answer to another question: What is an auto trait in Rust?
回答2:
This is a negative trait impl, so you can read it as opting out of the Send
trait.
来源:https://stackoverflow.com/questions/30291217/what-does-the-exclamation-point-mean-in-a-trait-implementation