@@ -4,33 +4,40 @@ In this section we'll write the smallest `#![no_std]` program that *compiles*.
4
4
5
5
## What does ` #![no_std] ` mean?
6
6
7
- ` #![no_std] ` is a crate level attribute that indicates that the crate will link to the ` core ` crate
8
- instead of to the ` std ` crate, but what does this mean for applications?
7
+ ` #![no_std] ` is a crate level attribute that indicates that the crate will link to the [ ` core ` ] crate
8
+ instead of the [ ` std ` ] crate, but what does this mean for applications?
9
+
10
+ [ `core` ] : https://doc.rust-lang.org/core/
11
+ [ `std` ] : https://doc.rust-lang.org/std/
9
12
10
13
The ` std ` crate is Rust's standard library. It contains functionality that assumes that the program
11
- will run on a operating system rather than * directly on the metal* . ` std ` also assumes that the
14
+ will run on an operating system rather than [ * directly on the metal* ] . ` std ` also assumes that the
12
15
operating system is a general purpose operating system, like the ones one would find in servers and
13
16
desktops. For this reason, ` std ` provides a standard API over functionality one usually finds in
14
- such operating systems: threads, files, sockets, a filesystem, processes, etc.
17
+ such operating systems: Threads, files, sockets, a filesystem, processes, etc.
18
+
19
+ [ *directly on the metal* ] : https://en.wikipedia.org/wiki/Bare_machine
15
20
16
21
On the other hand, the ` core ` crate is a subset of the ` std ` crate that makes zero assumptions about
17
22
the system the program will run on. As such, it provides APIs for language primitives like floats,
18
23
strings and slices, as well as APIs that expose processor features like atomic operations and SIMD
19
- instructions but it lacks APIs for anything that involves heap memory allocations and I/O.
24
+ instructions. However it lacks APIs for anything that involves heap memory allocations and I/O.
20
25
21
- For an application ` std ` does more than just provide a way to access OS abstractions. ` std ` also
26
+ For an application, ` std ` does more than just providing a way to access OS abstractions. ` std ` also
22
27
takes care of, among other things, setting up stack overflow protection, processing command line
23
28
arguments and spawning the main thread before a program's ` main ` function is invoked. A ` #![no_std] `
24
- application lacks all that standard runtime so it must initialize its own runtime, if any is
29
+ application lacks all that standard runtime, so it must initialize its own runtime, if any is
25
30
required.
26
31
27
- Because of these properties a ` #![no_std] ` application can be the first and / or the only thing that
28
- runs on a system; it can the kernel of an OS; it can be firmware; it can be a bootloader; it can be
29
- many things that a standard Rust application can never be.
32
+ Because of these properties, a ` #![no_std] ` application can be the first and / or the only code that
33
+ runs on a system. It can be many things that a standard Rust application can never be, for example:
34
+ - The kernel of an OS.
35
+ - Firmware.
36
+ - A bootloader.
30
37
31
38
## The code
32
39
33
- With that out of the way we can move onto the smallest ` #![no_std] ` program that compiles:
40
+ With that out of the way, we can move on to the smallest ` #![no_std] ` program that compiles:
34
41
35
42
``` console
36
43
$ cargo new --bin app
@@ -63,8 +70,8 @@ The `#![no_std]` attribute which we have already extensively covered.
63
70
64
71
The ` #![no_main] ` attribute which means that the program won't use the standard ` main ` function as
65
72
its entry point. At the time of writing, Rust's ` main ` interface makes some assumptions about the
66
- environment the program executes in: for example, it assumes the existence of command line
67
- arguments so, in general, it's not appropriate for ` #![no_std] ` programs.
73
+ environment the program executes in: For example, it assumes the existence of command line
74
+ arguments, so in general, it's not appropriate for ` #![no_std] ` programs.
68
75
69
76
The ` #[panic_implementation] ` attribute. The function marked with this attribute defines the
70
77
behavior of panics, both library level panics (` core::panic! ` ) and language level panics (out of
@@ -90,8 +97,8 @@ $ cargo nm -- ./target/thumbv7m-none-eabi/debug/deps/app-e223b941430cb13d.o | ta
90
97
00000000 T rust_begin_unwind
91
98
```
92
99
93
- However, it's our starting point. In the next section we'll build something useful. But before
94
- continuing let's set a default build target to avoid having to pass the ` --target ` flag to every
100
+ However, it's our starting point. In the next section, we'll build something useful. But before
101
+ continuing, let's set a default build target to avoid having to pass the ` --target ` flag to every
95
102
Cargo invocation.
96
103
97
104
``` console
0 commit comments