I'm currently in the progress of learning Rust. I'm mainly using The Rust Programming Language book and this nice reference which relates Rust features/syntax to C++ equivalents.
I'm having a hard time understanding where the core language stops and the standard library starts. I've encountered a lot of operators and/or traits which seems to have a special relationship with the compiler. For example, Rust has a trait
(which from what I understand is like an interface) called Deref
which let's a type implementing it be de-referenced using the *
operator:
fn main() {
let x = 5;
let y = Box::new(x);
assert_eq!(5, x);
assert_eq!(5, *y);
}
Another example is the ?
operator, which seems to depend on the Result
and Option
types.
Can code that uses those operators can be compiled without the standard library? And if not, what parts of the Rust language are depending on the standard library? Is it even possible to compile any Rust code without it?
Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed.
The Rust compiler uses a query system which is unlike most textbook compilers, which are organized as a series of passes over the code that execute sequentially.
no_std] is a crate-level attribute that indicates that the crate will link to the core-crate instead of the std-crate. The libcore crate in turn is a platform-agnostic subset of the std crate which makes no assumptions about the system the program will run on.
The minimum requirements for Rust toolchains targeting Linux will increase with the Rust 1.64. 0 release (slated for September 22nd, 2022). The new minimum requirements are: glibc >= 2.17 (previously glibc >= 2.11)
The Rust standard library is in fact separated into three distinct crates:
core
, which is the glue between the language and the standard library. All types, traits and functions required by the language are found in this crate. This includes operator traits (found in core::ops
), the Future
trait (used by async fn
), and compiler intrinsics. The core
crate does not have any dependencies, so you can always use it.alloc
, which contains types and traits related to or requiring dynamic memory allocation. This includes dynamically allocated types such as Box<T>
, Vec<T>
and String
.std
, which contains the whole standard library, including things from core
and alloc
but also things with further requirements, such as file system access, networking, etc.If your environment does not provide the functionality required by the std
crate, you can choose to compile without it. If your environment also does not provide dynamic memory allocation, you can choose to compile without the alloc
crate as well. This option is useful for targets such as embedded systems or writing operating systems, where you usually won't have all of the things that the standard library usually requires.
You can use the #![no_std]
attribute in the root of your crate to tell the compiler to compile without the standard library (only core
). Many libraries also usually support "no-std
" compilation (e.g. base64
and futures
), where functionality may be restricted but it will work when compiling without the std
crate.
DISCLAIMER: This is likely not the answer you're looking for. Consider reading the other answers about no_std
, if you're trying to solve a problem. I suggest you only read on, if you're interested in trivia about the inner workings of Rust.
If you really want full control over the environment you use, it is possible to use Rust without the core
library using the no_core
attribute.
If you decide to do so, you will run into some problems, because the compiler is integrated with some items defined in core
.
This integration works by applying the #[lang = "..."]
attribute to those items, making them so called "lang items".
If you use no_core
, you'll have to define your own lang items for the parts of the language you'll actually use.
For more information I suggest the following blog posts, which go into more detail on the topic of lang items and no_core
:
So yes, in theory it is possible to run Rust code without any sort of standard library and supplied types, but only if you then supply the required types yourself. Also this is not stable and will likely never be stabilized and it is generally not a recommended way of using Rust.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With