]> git.proxmox.com Git - rustc.git/blob - src/doc/book/using-rust-without-the-standard-library.md
New upstream version 1.16.0+dfsg1
[rustc.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 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/index.html). When we’re using the
26 standard library, Rust automatically brings `std` into scope, allowing you to
27 use 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/index.html). This means that a lot of code will Just
30 Work:
31
32 ```rust
33 #![no_std]
34
35 fn may_fail(failure: bool) -> Result<(), &'static str> {
36 if failure {
37 Err("this didn’t work!")
38 } else {
39 Ok(())
40 }
41 }
42 ```