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.
Should one default to using std::ops::Add
? Why do both, as opposed to one, of them exist?
They're in fact exactly the same, despite the set of implementors being listed as slightly different.
The
core
library is designed for bare-metal/low-level tasks, and is thus more barebones than whatstd
can provide by assuming an operating system exists. However, people usingstd
will want the stuff that's incore
too (e.g.Add
orOption
or whatever), and so to avoid having to load bothstd
andcore
,std
reexports everything fromcore
, viapub use
. That is,std
provides aliases/import paths for the things incore
.There are some unfortunate error messages where the compiler points to the original source of an item, not the reexport, which might not be in a crate you're
extern crate
ing.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 fromstd
, and virtually always under exactly the same path (i.e., you can just replace the "core" prefix with "std").As for which one you should use: If you have a reason to not link to the standard library (
#![no_std]
), then thestd::*
one isn't available so obviously you usecore::*
. If on the other hand you do use the standard library, you should use thestd::*
re-export. It is more customary and requires less typing.