Skip to content

Provide a note on 'extern crate' usage in edition 2018 syntax of Rust #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/start/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ times it has been called in the `COUNT` variable and then prints the value of
#![no_main]
#![no_std]

extern crate panic_halt;
use panic_halt as _;

use core::fmt::Write;

Expand Down Expand Up @@ -190,7 +190,7 @@ memory location.
#![no_main]
#![no_std]

extern crate panic_halt;
use panic_halt as _;

use core::fmt::Write;
use core::ptr;
Expand Down
13 changes: 10 additions & 3 deletions src/start/panicking.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ profile. For example:

// dev profile: easier to debug panics; can put a breakpoint on `rust_begin_unwind`
#[cfg(debug_assertions)]
extern crate panic_halt;
use panic_halt as _;

// release profile: minimize the binary size of the application
#[cfg(not(debug_assertions))]
extern crate panic_abort;
use panic_abort as _;

// ..
```
Expand All @@ -64,6 +64,13 @@ In this example the crate links to the `panic-halt` crate when built with the
dev profile (`cargo build`), but links to the `panic-abort` crate when built
with the release profile (`cargo build --release`).

> The `use panic_abort as _;` form of the `use` statement is used to ensure the `panic_abort` panic handler is
> included in our final executable while making it clear to the compiler that we won't explicitly use anything from
> the crate. Without the `as _` rename, the compiler would warn that we have an unused import.
> Sometimes you might see `extern crate panic_abort` instead, which is an older style used before the
> 2018 edition of Rust, and should now only be used for "sysroot" crates (those distributed with Rust itself) such
> as `proc_macro`, `alloc`, `std`, and `test`.

## An example

Here's an example that tries to index an array beyond its length. The operation
Expand All @@ -73,7 +80,7 @@ results in a panic.
#![no_main]
#![no_std]

extern crate panic_semihosting;
use panic_semihosting as _;

use cortex_m_rt::entry;

Expand Down
8 changes: 4 additions & 4 deletions src/start/qemu.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ For convenience here are the most important parts of the source code in `src/mai
#![no_std]
#![no_main]

extern crate panic_halt;
use panic_halt as _;

use cortex_m_rt::entry;

Expand All @@ -117,7 +117,7 @@ interface that most Rust programs use. The main (no pun intended) reason to go
with `no_main` is that using the `main` interface in `no_std` context requires
nightly.

`extern crate panic_halt;`. This crate provides a `panic_handler` that defines
`use panic_halt as _;`. This crate provides a `panic_handler` that defines
the panicking behavior of the program. We will cover this in more detail in the
[Panicking](panicking.md) chapter of the book.

Expand Down Expand Up @@ -318,7 +318,7 @@ For convenience here's the source code of `examples/hello.rs`:
#![no_main]
#![no_std]

extern crate panic_halt;
use panic_halt as _;

use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
Expand Down Expand Up @@ -509,7 +509,7 @@ list main
This will show the source code, from the file examples/hello.rs.

```text
6 extern crate panic_halt;
6 use panic_halt as _;
7
8 use cortex_m_rt::entry;
9 use cortex_m_semihosting::{debug, hprintln};
Expand Down
4 changes: 2 additions & 2 deletions src/start/registers.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ We won't get very far with our embedded software development if we restrict ours
#![no_std]
#![no_main]

extern crate panic_halt; // panic handler
use panic_halt as _; // panic handler

use cortex_m_rt::entry;
use tm4c123x;
Expand Down Expand Up @@ -129,7 +129,7 @@ Let's see an example:
#![no_std]
#![no_main]

extern crate panic_halt; // panic handler
use panic_halt as _; // panic handler

use cortex_m_rt::entry;
use tm4c123x_hal as hal;
Expand Down
6 changes: 3 additions & 3 deletions src/start/semihosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ world!":
#![no_main]
#![no_std]

extern crate panic_halt;
use panic_halt as _;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
Expand Down Expand Up @@ -67,7 +67,7 @@ until you restart it.
#![no_main]
#![no_std]

extern crate panic_halt;
use panic_halt as _;

use cortex_m_rt::entry;
use cortex_m_semihosting::debug;
Expand Down Expand Up @@ -105,7 +105,7 @@ stderr.
#![no_main]
#![no_std]

extern crate panic_semihosting; // features = ["exit"]
use panic_semihosting as _; // features = ["exit"]

use cortex_m_rt::entry;
use cortex_m_semihosting::debug;
Expand Down