Skip to content

Commit 8d7463f

Browse files
Merge pull request rust-lang#415 from Havvy/no_std
Document preludes
2 parents f87f140 + 61d5682 commit 8d7463f

File tree

4 files changed

+103
-42
lines changed

4 files changed

+103
-42
lines changed

src/attributes.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ names have meaning.
107107

108108
## Crate-only attributes
109109

110+
> **Note**: This section is in the process of being removed, with specific
111+
> sections for each attribute. It is not the full list of crate-root attributes.
112+
110113
- `crate_name` - specify the crate's crate name.
111114
- `crate_type` - see [linkage](linkage.html).
112115
- `no_builtins` - disable optimizing certain code patterns to invocations of
@@ -115,7 +118,6 @@ names have meaning.
115118
object being linked to defines `main`.
116119
- `no_start` - disable linking to the `native` crate, which specifies the
117120
"start" language item.
118-
- `no_std` - disable linking to the `std` crate.
119121
- `recursion_limit` - Sets the maximum depth for potentially
120122
infinitely-recursive compile-time operations like
121123
auto-dereference or macro expansion. The default is
@@ -130,14 +132,6 @@ names have meaning.
130132

131133
[subsystem]: https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx
132134

133-
## Module-only attributes
134-
135-
- `no_implicit_prelude` - disable injecting `use std::prelude::*` in this
136-
module.
137-
- `path` - specifies the file to load the module from. `#[path="foo.rs"] mod
138-
bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`. The path is
139-
taken relative to the directory that the current module is in.
140-
141135
## FFI attributes
142136

143137
On an `extern` block, the following attributes are interpreted:
@@ -245,6 +239,17 @@ are transformed into `doc` attributes.
245239

246240
See [The Rustdoc Book] for reference material on this attribute.
247241

242+
### `path`
243+
244+
The `path` attribute says where a [module]'s source file is. See [modules] for
245+
more information.
246+
247+
### Preludes
248+
249+
The [prelude] behavior can be modified with attributes. The [`no_std`] attribute
250+
changes the prelude to the core prelude while the [`no_implicit_prelude`]
251+
prevents the prelude from being added to the module.
252+
248253
### Testing
249254

250255
The compiler comes with a default test framework. It works by attributing
@@ -556,8 +561,12 @@ You can implement `derive` for your own traits through [procedural macros].
556561

557562
[_LiteralExpression_]: expressions/literal-expr.html
558563
[_SimplePath_]: paths.html#simple-paths
564+
[`no_implicit_prelude`]: items/modules.html
565+
[`no_std`]: crates-and-source-files.html#preludes-and-no_std
559566
[Doc comments]: comments.html#doc-comments
560567
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
568+
[module]: items/modules.html
569+
[prelude]: crates-and-source-files.html#preludes-and-no_std
561570
[procedural macros]: procedural-macros.html
562571
[struct]: items/structs.html
563572
[enum]: items/enumerations.html

src/crates-and-source-files.md

+56-25
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
Rust's semantics obey a *phase distinction* between compile-time and
2121
run-time.[^phase-distinction] Semantic rules that have a *static
2222
interpretation* govern the success or failure of compilation, while
23-
semantic rules
24-
that have a *dynamic interpretation* govern the behavior of the program at
25-
run-time.
23+
semantic rules that have a *dynamic interpretation* govern the behavior of the
24+
program at run-time.
2625

2726
The compilation model centers on artifacts called _crates_. Each compilation
2827
processes a single crate in source form, and if successful, produces a single
@@ -66,22 +65,6 @@ apply to the crate as a whole.
6665
#![warn(non_camel_case_types)]
6766
```
6867

69-
A crate that contains a `main` [function] can be compiled to an executable. If a
70-
`main` function is present, it must take no arguments, must not declare any
71-
[trait or lifetime bounds], must not have any [where clauses], and its return
72-
type must be one of the following:
73-
74-
* `()`
75-
* `Result<(), E> where E: Error`
76-
<!-- * `!` -->
77-
<!-- * Result<!, E> where E: Error` -->
78-
79-
> Note: The implementation of which return types are allowed is determined by
80-
> the unstable [`Termination`] trait.
81-
82-
<!-- If the previous section needs updating (from "must take no arguments"
83-
onwards, also update it in the attributes.md file, testing section -->
84-
8568
The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
8669
file is encoded in UTF8. It can only occur at the beginning of the file and
8770
is ignored by the compiler.
@@ -100,6 +83,48 @@ fn main() {
10083
}
10184
```
10285

86+
## Preludes and `no_std`
87+
88+
All crates have a *prelude* that automatically inserts names from a specific
89+
module, the *prelude module*, into scope of each [module] and an [`extern
90+
crate]` into the crate root module. By default, the *standard prelude* is used.
91+
The linked crate is [`std`] and the prelude module is [`std::prelude::v1`].
92+
93+
The prelude can be changed to the *core prelude* by using the `no_std`
94+
[attribute] on the root crate module. The linked crate is [`core`] and the
95+
prelude module is [`core::prelude::v1`]. Using the core prelude over the
96+
standard prelude is useful when either the crate is targeting a platform that
97+
does not support the standard library or is purposefully not using the
98+
capabilities of the standard library. Those capabilities are mainly dynamic
99+
memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g.
100+
`std::fs` and `std::io`).
101+
102+
<div class="warning">
103+
104+
Warning: Using `no_std` does not prevent the standard library from being linked
105+
in. It is still valid to put `extern crate std;` into the crate and dependencies
106+
can also link it in.
107+
108+
</div>
109+
110+
## Main Functions
111+
112+
A crate that contains a `main` [function] can be compiled to an executable. If a
113+
`main` function is present, it must take no arguments, must not declare any
114+
[trait or lifetime bounds], must not have any [where clauses], and its return
115+
type must be one of the following:
116+
117+
* `()`
118+
* `Result<(), E> where E: Error`
119+
<!-- * `!` -->
120+
<!-- * Result<!, E> where E: Error` -->
121+
122+
> Note: The implementation of which return types are allowed is determined by
123+
> the unstable [`Termination`] trait.
124+
125+
<!-- If the previous section needs updating (from "must take no arguments"
126+
onwards, also update it in the attributes.md file, testing section -->
127+
103128
[^phase-distinction]: This distinction would also exist in an interpreter.
104129
Static checks like syntactic analysis, type checking, and lints should
105130
happen before the program is executed regardless of when it is executed.
@@ -108,15 +133,21 @@ fn main() {
108133
ECMA-335 CLI model, a *library* in the SML/NJ Compilation Manager, a *unit*
109134
in the Owens and Flatt module system, or a *configuration* in Mesa.
110135

111-
[module]: items/modules.html
112-
[module path]: paths.html
113-
[attributes]: attributes.html
114-
[unit]: types.html#tuple-types
115136
[_InnerAttribute_]: attributes.html
116137
[_Item_]: items.html
117138
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
118139
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
119-
[function]: items/functions.html
120140
[`Termination`]: ../std/process/trait.Termination.html
121-
[where clauses]: items/generics.html#where-clauses
141+
[`core`]: ../core/index.html
142+
[`core::prelude::v1`]: ../core/preludce.index.html
143+
[`std`]: ../std/index.html
144+
[`std::prelude::v1`]: ../std/prelude/index.html
145+
[`use` declaration]: items/use-declarations.html
146+
[attribute]: attributes.html
147+
[attributes]: attributes.html
148+
[function]: items/functions.html
149+
[module]: items/modules.html
150+
[module path]: paths.html
122151
[trait or lifetime bounds]: trait-bounds.html
152+
[unit]: types.html#tuple-types
153+
[where clauses]: items/generics.html#where-clauses

src/items/functions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ fn test_only() {
209209
The attributes that have meaning on a function are [`cfg`], [`deprecated`],
210210
[`doc`], `export_name`, `link_section`, `no_mangle`, [the lint check
211211
attributes], [`must_use`], [the procedural macro attributes], [the testing
212-
attributes], and [the optimization hint
213-
attributes].
212+
attributes], and [the optimization hint attributes]. Functions also accept
213+
attributes macros.
214214

215215
[IDENTIFIER]: identifiers.html
216216
[RAW_STRING_LITERAL]: tokens.html#raw-string-literals

src/items/modules.md

+27-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ mod math {
2121
type Complex = (f64, f64);
2222
fn sin(f: f64) -> f64 {
2323
/* ... */
24-
# panic!();
24+
# unimplemented!();
2525
}
2626
fn cos(f: f64) -> f64 {
2727
/* ... */
28-
# panic!();
28+
# unimplemented!();
2929
}
3030
fn tan(f: f64) -> f64 {
3131
/* ... */
32-
# panic!();
32+
# unimplemented!();
3333
}
3434
}
3535
```
@@ -68,10 +68,31 @@ mod thread {
6868
}
6969
```
7070

71-
[IDENTIFIER]: identifiers.html
71+
Modules implicitly have some names in scope. These name are to built-in types,
72+
macros imported with `#[macro_use]` on an extern crate, and by the crate's
73+
[prelude]. These names are all made of a single identifier. These names are not
74+
part of the module, so for example, any name `name`, `self::name` is not a
75+
valid path. The names added by the [prelude] can be removed by placing the
76+
`no_implicit_prelude` [attribute] onto the module.
7277

73-
[_InnerAttribute_]: attributes.html
74-
[_OuterAttribute_]: attributes.html
78+
## Attributes on Modules
79+
80+
Modules, like all items, accept outer attributes. They also accept inner
81+
attibutes: either after `{` for a module with a body, or at the beginning of the
82+
source file, after the optional BOM and shebang.
7583

84+
The built-in attributes that have meaning on a function are [`cfg`],
85+
[`deprecated`], [`doc`], [the lint check attributes], `path`, and
86+
`no_implicit_prelude`. Modules also accept macro attributes.
87+
88+
[_InnerAttribute_]: attributes.html
7689
[_Item_]: items.html
90+
[_OuterAttribute_]: attributes.html
91+
[`cfg`]: conditional-compilation.html
92+
[`deprecated`]: attributes.html#deprecation
93+
[`doc`]: attributes.html#documentation
94+
[IDENTIFIER]: identifiers.html
95+
[attribute]: attributes.html
7796
[items]: items.html
97+
[prelude]: crates-and-source-files.html#preludes-and-no_std
98+
[the lint check attributes]: attributes.html#lint-check-attributes

0 commit comments

Comments
 (0)