问题描述
这两个特征(std::ops::Add, core::ops::Add) 提供相同的功能,并且它们都使用相同的示例(都使用 std::ops::Add
).他们的实现者集有所不同.
These two traits (std::ops::Add, core::ops::Add) provide the same functionality, and they both use the same example (both utilize std::ops::Add
). Their set of implementors differ somewhat.
是否应该默认使用std::ops::Add
?为什么两者都存在,而不是其中一个?
Should one default to using std::ops::Add
? Why do both, as opposed to one, of them exist?
推荐答案
没有两个特征.有一种特征以多个可互换的名称导出.这远非独一无二.实际上 core
中的所有内容也都是从 std
导出的,并且几乎总是在完全相同的路径下(即,您可以将core"前缀替换为std").
There aren't two traits. There is one trait which is exported under several interchangeable names. This is far from unique. Virtually everything in core
is also exported from std
, and virtually always under exactly the same path (i.e., you can just replace the "core" prefix with "std").
至于您应该使用哪个:如果您有理由不链接到标准库 (#![no_std]
),那么 std::*
一个不可用,所以很明显你使用 core::*
.另一方面,如果您确实使用标准库,则应该使用 std::*
重新导出.它更习惯,需要更少的打字.
As for which one you should use: If you have a reason to not link to the standard library (#![no_std]
), then the std::*
one isn't available so obviously you use core::*
. If on the other hand you do use the standard library, you should use the std::*
re-export. It is more customary and requires less typing.
这篇关于std::ops::Add 还是 core::ops::Add ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!