rust/src/doc/book/using-rust-without-the-standard-library.md
2016-02-28 20:46:56 -05:00

1.4 KiB
Raw Blame History

% Using Rust Without the Standard Library

Rusts standard library provides a lot of useful functionality, but assumes support for various features of its host system: threads, networking, heap allocation, and others. There are systems that do not have these features, however, and Rust can work with those too! To do so, we tell Rust that we dont want to use the standard library via an attribute: #![no_std].

Note: This feature is technically stable, but there are some caveats. For one, you can build a #![no_std] library on stable, but not a binary. For details on binaries without the standard library, see the nightly chapter on #![no_std]

To use #![no_std], add it to your crate root:

#![no_std]

fn plus_one(x: i32) -> i32 {
    x + 1
}

Much of the functionality thats exposed in the standard library is also available via the core crate. When were using the standard library, Rust automatically brings std into scope, allowing you to use its features without an explicit import. By the same token, when using #![no_std], Rust will bring core into scope for you, as well as its prelude. This means that a lot of code will Just Work:

#![no_std]

fn may_fail(failure: bool) -> Result<(), &'static str> {
    if failure {
        Err("this didnt work!")
    } else {
        Ok(())
    }
}