How does Rust implicitly include the `core` and `std` libraries for compilation?

Hi folks!

Where does Rust store the core and std libraries to guarantee that they're accessible to rustc and cargo when compiling Rust projects?

Does rustc know about std and core? Or does cargo implicitly include these at build time as if they're any other crate?

Context

I'm hacking on a domain-specific programming language (very much inspired by Rust) and we've reached the point where we're ready to include the language's core and std libraries implicitly (currently, users have to manually specify std and core dependencies in their manifest). We don't have a prelude module yet, but to begin we just want to start by ensuring that core and std are accessible without having to manually declare them as dependencies, and this got me curious about Rust's approach :thinking:

For the most part, rustc just inherently knows about them, and finds compiled versions of them in the sysroot.

However, I would recommend learning from what we're moving towards rather than what we currently do. Just add the logic to always build core and std from source, exactly like any other package, and then separately add logic for caching compiled binaries. Don't special-case the standard library; you may end up regretting that special case later, and it may be hard to fix.

14 Likes

prelude

I am pretty new to rust but the standard prelude handles most of that I think.

Is the plan to install a copy of each crate's src in a common, local user directory during installation? And then perhaps sync their major.minor version with rustc so that cargo knows which to use?

I'd imagine the implicit core and std crates won't be fetched from crates.io or the rust git repo in order to ensure that folks can build new projects offline.

Yeah, we'd still provide a local copy of the source that exactly matches the version of rustc.

1 Like

Here's work on moving towards std being more like a regular crate:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.