Why is the str datatype in rust declared in three different spaces?

why is the str datatype declared in three different spaces ?

core - alloc - std

why what is the point ?

1 Like

The ones from alloc and std are just reexports of core for convenience.

3 Likes

so str is originally from core ?

1 Like

That is what reexports tells us.

2 Likes

It's a primitive of the language, more like.

3 Likes

how do i know if something is re-exported ?

1 Like

Copying over my answer from the duplicate on irlo in case it's useful:

Usually, reexports are documented as such, like in the prelude modules where they show up under a "Re-exports" header and are displayed as a pub use. For #[doc(inline)]d items, there's no (immediate[4]) indication in the docs whether an item is reexported or actually defined inline.

For the std facade specifically, it's by convention that items are reexported at the same path. If some item is available at both core::path::to::item and std::path::to::item, it's the same item, barring some niche historical edge cases[5].


  1. Caveat: some things like core::arch and core::atomic are only conditionally available based on architecture support. ↩︎

  2. noun: facade; an outward appearance that is maintained to conceal a less pleasant or creditable reality. ↩︎

  3. This can have benefits to compile time (if the crate dependency graph is wide, it enables further parallelism) and for crates that only need a subset of functionality to only depend on that subset, even if the entire package is included into the build tree (which allows better build pipelining by not waiting on building functionality a library doesn't need as well as core types being versioned more stably than the veneer) ↩︎

  4. If you click the source link, it takes you to the actual item definition, so you can know by looking at the path of the defining file, e.g. /src/core/... or /src/std/.... And if the source link isn't available but is for other items (e.g. on docs-rs), that's usually indication that it's a cross-crate reexport. ↩︎

  5. The panic! macro had subtly different behavior between core::panic! and std::panic! before the 2021 edition. This can mostly be attributed to macros not being useable early in Rust's lifetime and core not having access to format!, meaning the two macros had to be separately implemented with different functionality at the time. ↩︎

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.