感叹号在 trait 实现中是什么意思? [英] What does the exclamation point mean in a trait implementation?

查看:56
本文介绍了感叹号在 trait 实现中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在库参考中找到了 std::rc::Rc 这个 trait实施

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

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

Send前面的感叹号是什么意思?

What does the exclamation point in front of Send mean?

我查阅了 The Rust Programming Language¹ 书籍和 The Rust Reference²,但没有找到解释.请在您的回答中提供参考.

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.


¹ 尤其是 [section 3.19特征
² 和部分 5.1 Traits5.1 实现

推荐答案

RFC 19.

总结:Send trait 是一个 auto trait,这意味着它会自动为所有只包含其他 Send 的类型实现类型:

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 也是一个 unsafe trait,这意味着实现它是不安全的,但这与问题无关.)

(Send is also an unsafe trait, which means it is unsafe to implement, but that is not relevant to the question.)

auto trait 不能定义任何方法,这也使它成为一个 marker trait.(定义自动特征的语法目前仅在标准库或 nightly 编译器中可用,但它们的语义是稳定的.)

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

要选择退出 Send 的自动实现,您必须编写一个明确的否定 trait 实现:

To opt out of the automatic implementation of Send, you must write an explicit negative trait implementation:

impl !Send for MyType {}

这意味着即使MyType只包含Send的其他类型,MyType本身不会自动实现Send.

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

另请参阅另一个问题的答案:Rust 中的 auto trait 是什么?

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

这篇关于感叹号在 trait 实现中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆