Skip to content

Commit 48ebec7

Browse files
authored
Merge pull request #9 from andre-richter/master
Review
2 parents 8a4b619 + ab3371a commit 48ebec7

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/smallest-no-std.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,40 @@ In this section we'll write the smallest `#![no_std]` program that *compiles*.
44

55
## What does `#![no_std]` mean?
66

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/
912

1013
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
1215
operating system is a general purpose operating system, like the ones one would find in servers and
1316
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
1520

1621
On the other hand, the `core` crate is a subset of the `std` crate that makes zero assumptions about
1722
the system the program will run on. As such, it provides APIs for language primitives like floats,
1823
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.
2025

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
2227
takes care of, among other things, setting up stack overflow protection, processing command line
2328
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
2530
required.
2631

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.
3037

3138
## The code
3239

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:
3441

3542
``` console
3643
$ cargo new --bin app
@@ -63,8 +70,8 @@ The `#![no_std]` attribute which we have already extensively covered.
6370

6471
The `#![no_main]` attribute which means that the program won't use the standard `main` function as
6572
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.
6875

6976
The `#[panic_implementation]` attribute. The function marked with this attribute defines the
7077
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
9097
00000000 T rust_begin_unwind
9198
```
9299

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
95102
Cargo invocation.
96103

97104
``` console

0 commit comments

Comments
 (0)