]> git.lizzy.rs Git - rust.git/blob - src/doc/book/using-rust-without-the-standard-library.md
Move some #[no_std] info to stable book.
[rust.git] / src / doc / book / using-rust-without-the-standard-library.md
1 % Using Rust Without the Standard Library
2
3 Rust’s standard library provides a lot of useful functionality, but assumes
4 support for various features of its host system: threads, networking, heap
5 allocation, and others. There are systems that do not have these features,
6 however, and Rust can work with those too! To do so, we tell Rust that we
7 don’t want to use the standard library via an attribute: `#![no_std]`.
8
9 > Note: This feature is technically stable, but there are some caveats. For
10 > one, you can build a `#![no_std]` _library_ on stable, but not a _binary_.
11 > For details on binaries without the standard library, see [the nightly
12 > chapter on `#![no_std]`](no-stdlib.html)
13
14 To use `#![no_std]`, add a it to your crate root:
15
16 ```rust
17 #![no_std]
18
19 fn plus_one(x: i32) -> i32 {
20     x + 1
21 }
22 ```
23
24 Much of the functionality that’s exposed in the standard library is also
25 available via the [`core` crate](../core/). When we’re using the standard
26 library, Rust automatically brings `std` into scope, allowing you to use
27 its features without an explicit import. By the same token, when using
28 `!#[no_std]`, Rust will bring `core` into scope for you, as well as [its
29 prelude](../core/prelude/v1/). This means that a lot of code will Just Work:
30
31 ```rust
32 #![no_std]
33
34 fn may_fail(failure: bool) -> Result<(), &'static str> {
35     if failure {
36         Err("this didn’t work!")
37     } else {
38         Ok(())
39     }
40 }
41 ```