Skip to content

Commit bbb5cc6

Browse files
authored
Merge pull request rust-lang#1913 from ehuss/no_std
Update `no_std` to use the attribute template
2 parents 0e9e11d + 97f3e33 commit bbb5cc6

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/names/preludes.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,46 @@ See https://github.com/rust-lang/rust/issues/57288 for more about the
7676
alloc/test limitation.
7777
-->
7878
79+
<!-- template:attributes -->
7980
r[names.preludes.extern.no_std]
8081
### The `no_std` attribute
8182
8283
r[names.preludes.extern.no_std.intro]
83-
By default, the standard library is automatically included in the crate root
84-
module. The [`std`] crate is added to the root, along with an implicit
85-
[`macro_use` attribute] pulling in all macros exported from `std` into the
86-
[`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern
87-
prelude].
84+
The *`no_std` [attribute][attributes]* causes the [`std`] crate to not be linked automatically, the [standard library prelude] to instead use the `core` prelude, and the [`macro_use` prelude] to instead use the macros exported from the `core` crate.
85+
86+
> [!EXAMPLE]
87+
> <!-- ignore: test infrastructure can't handle no_std -->
88+
> ```rust,ignore
89+
> #![no_std]
90+
> ```
91+
92+
> [!NOTE]
93+
> Using `no_std` is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`).
94+
95+
> [!WARNING]
96+
> Using `no_std` does not prevent the standard library from being linked. It is still valid to write `extern crate std` in the crate or in one of its dependencies; this will cause the compiler to link the `std` crate into the program.
97+
98+
r[names.preludes.extern.no_std.syntax]
99+
The `no_std` attribute uses the [MetaWord] syntax.
88100
89101
r[names.preludes.extern.no_std.allowed-positions]
90-
The *`no_std` [attribute]* may be applied at the crate level to prevent the
91-
[`std`] crate from being automatically added into scope.
102+
The `no_std` attribute may only be applied to the crate root.
92103
93-
It does three things:
104+
r[names.preludes.extern.no_std.duplicates]
105+
The `no_std` attribute may be used any number of times on a form.
106+
107+
> [!NOTE]
108+
> `rustc` lints against any use following the first.
94109
95-
r[names.preludes.extern.no_std.extern]
96-
* Prevents `std` from being added to the [extern prelude](#extern-prelude).
97110
r[names.preludes.extern.no_std.module]
98-
* Affects which module is used to make up the [standard library prelude] (as described above).
99-
r[names.preludes.extern.no_std.core]
100-
* Injects the [`core`] crate into the crate root instead of [`std`], and pulls
101-
in all macros exported from `core` in the [`macro_use` prelude].
111+
The `no_std` attribute changes the [standard library prelude] to use the `core` prelude instead of the `std` prelude.
102112
103-
> [!NOTE]
104-
> Using the core prelude over the standard prelude is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`).
113+
r[names.preludes.extern.no_std.macro_use]
114+
By default, all macros exported from the `std` crate are added to the [`macro_use` prelude]. If the `no_std` attribute is specified, then all macros exported from the `core` crate are placed into the [`macro_use` prelude] instead.
105115
106-
> [!WARNING]
107-
> Using `no_std` does not prevent the standard library from being linked in. It is still valid to put `extern crate std;` into the crate and dependencies can also link it in.
116+
r[names.preludes.extern.no_std.edition2018]
117+
> [!EDITION-2018]
118+
> Before the 2018 edition, `std` is injected into the crate root by default. If `no_std` is specified, `core` is injected instead. Starting with the 2018 edition, regardless of `no_std` being specified, neither is injected into the crate root.
108119
109120
r[names.preludes.lang]
110121
## Language prelude
@@ -160,7 +171,6 @@ r[names.preludes.no_implicit_prelude.edition2018]
160171
[`macro_use` attribute]: ../macros-by-example.md#the-macro_use-attribute
161172
[`macro_use` prelude]: #macro_use-prelude
162173
[`no_std` attribute]: #the-no_std-attribute
163-
[`no_std` attribute]: #the-no_std-attribute
164174
[attribute]: ../attributes.md
165175
[Boolean type]: ../types/boolean.md
166176
[Built-in attributes]: ../attributes.md#built-in-attributes-index
@@ -171,7 +181,7 @@ r[names.preludes.no_implicit_prelude.edition2018]
171181
[Machine-dependent integer types]: ../types/numeric.md#machine-dependent-integer-types
172182
[Macro namespace]: namespaces.md
173183
[name resolution]: name-resolution.md
174-
[Standard library prelude]: #standard-library-prelude
184+
[standard library prelude]: names.preludes.std
175185
[Textual types]: ../types/textual.md
176186
[tool attributes]: ../attributes.md#tool-attributes
177187
[Tool prelude]: #tool-prelude

0 commit comments

Comments
 (0)