|
| 1 | +Policy for inclusion in the prelude |
| 2 | +=================================== |
| 3 | + |
| 4 | +The Rust standard library provides a "prelude": items that Rust programs can |
| 5 | +use without having to import anything. For instance, the Rust prelude includes |
| 6 | +[`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html), so programs can |
| 7 | +write `Vec::with_capacity(4)` without having to first import `Vec` from the |
| 8 | +standard library. |
| 9 | + |
| 10 | +Each edition of Rust has its own prelude, so that new editions of Rust may add |
| 11 | +items without making those items available to all code in older editions of |
| 12 | +Rust. |
| 13 | + |
| 14 | +This policy sets out the requirements for what items we add to the prelude, and |
| 15 | +whether we add those items to the prelude for a future edition or to the common |
| 16 | +prelude for all Rust editions. |
| 17 | + |
| 18 | +When to use editions |
| 19 | +-------------------- |
| 20 | + |
| 21 | +Adding a trait to the prelude can break existing Rust code, by creating name |
| 22 | +resolution ambiguity where none previously existed. While introducing |
| 23 | +resolution ambiguities may be "permitted breakage", doing so is quite |
| 24 | +disruptive, and we want to avoid doing so. Thus, we generally never add a trait |
| 25 | +to the common prelude; we only add traits to the prelude for a new edition of |
| 26 | +Rust. |
| 27 | + |
| 28 | +Adding items *other* than traits to the prelude will never produce conflicts or |
| 29 | +other compatibility issues with existing code, since we allow shadowing and |
| 30 | +give other sources of names priority over names from the prelude. Thus, if we |
| 31 | +choose to add a non-trait item to the prelude, we should typically add it to |
| 32 | +the common prelude for all editions of Rust. (Exceptions to this would include |
| 33 | +names that form part of an edition transition, such that the same name resolves |
| 34 | +to something different in different editions.) |
| 35 | + |
| 36 | +Criteria for including an item |
| 37 | +------------------------------ |
| 38 | + |
| 39 | +An item included in the prelude can be used by its unqualified name, by any |
| 40 | +Rust code. Users can look up the item by name, and easily get documentation for |
| 41 | +it. However, in general the name should make sense without any context, such as |
| 42 | +within a diff hunk or code sample. Any name we include in the prelude should be |
| 43 | +one that will not confuse users if they see it unqualified, without anything |
| 44 | +introducing the name. |
| 45 | + |
| 46 | +In particular, the name should not be something users are likely to |
| 47 | +misunderstand as coming from the local crate or its dependencies. While any |
| 48 | +crate *could* define any name, the names in the prelude should be *unlikely* to |
| 49 | +occur unqualified in another crate. (A conflict with a name typically used |
| 50 | +qualified or in a different context is not a problem; for instance, a common |
| 51 | +method name `Object::name`, commonly called via `expr.name`, does not |
| 52 | +necessarily preclude adding a free function `name` to the prelude.) |
| 53 | + |
| 54 | +We should only add an item to the prelude if some reasonable number of crates |
| 55 | +are likely to use the item. It need not be an item used by the *majority* of |
| 56 | +crates, but it should be reasonably frequent across the ecosystem. |
0 commit comments