What does the exclamation point mean in a trait implementation?

前端 未结 2 1397
我在风中等你
我在风中等你 2021-02-12 20:51

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

impl !Send for Rc 
where
    T: ?Sized, 

相关标签:
2条回答
  • 2021-02-12 21:03

    This is a negative trait impl, so you can read it as opting out of the Send trait.

    0 讨论(0)
  • 2021-02-12 21:09

    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?

    0 讨论(0)
提交回复
热议问题