In Rust, what is an edition, how does a crate choose one, and why does 2015-edition code still compile on rustc 1.99?
answer
- a language mode, not a compiler
- one setting per crate
- rustc's --edition flag
- async: a name in 2015, a keyword later
basics
~20 sAn edition is a language mode chosen per crate (2015, 2018, 2021 or 2024). Changes that would break old code, such as new keywords, land only in a new edition, so a crate keeps compiling until it opts in.
solid answer
~40 sAn edition is a per-crate language mode that rustc receives as `--edition`; Cargo passes the `edition` value from the crate's manifest, which defaults to 2015 when the key is absent, and `cargo new` writes the newest stable edition, 2024. Rust 1.99 accepts all four editions: 2015, 2018 (stable since 1.31), 2021 (1.56) and 2024 (1.85). A backwards-incompatible change, such as `async` and `dyn` becoming keywords in 2018 or `gen` being reserved in 2024, is attached to a new edition, so upgrading the compiler never moves your crate onto it: you migrate by changing the crate's edition. Most new features, including new std APIs, arrive on every edition; only changes that would break existing code wait for an edition boundary.
code
rust · 5 linesfn main() {
let async = 3; // an ordinary name before the 2018 edition
let total = async * 2;
println!("{}", total);
}go deeper
Recall that an edition is chosen per crate, that the four editions are 2015, 2018, 2021 and 2024, and that new keywords are the classic example of an edition-only change.
Explain how the edition reaches the compiler, the --edition flag that Cargo fills in from the manifest, the 2015 default, and why most features still land on every edition.
Show you can predict which changes need an edition boundary and which do not, and that a crate can sit on an old edition safely while still getting new std APIs and compiler fixes.
Frame editions as how Rust keeps its stability promise while still fixing past mistakes, and why that makes edition upgrades a planned, per-crate decision rather than a toolchain event.
## What an edition is Rust promises that code which compiles on a stable release keeps compiling on later releases. Some improvements cannot keep that promise: a new keyword, for example, breaks every program that already uses the word as a variable name. An **edition** is the escape hatch. It is a named **language mode** — `2015`, `2018`, `2021` or `2024` — and a backwards-incompatible change is published *inside* a new edition instead of being applied to everyone. Editions are **opt-in**. A crate that never changes its edition never sees the incompatible changes, no matter how new the compiler is. ## Why Rust needs editions at all Since 1.0, in May 2015, Rust has committed to "stability without stagnation": once a feature is stable, it keeps working in every later release. Without editions there would be only two ways to make `async` a keyword: break every crate that already used `async` as a name, or never add the keyword. Editions add a third option. The change is real, but each crate takes it when it is ready, and the move is largely automated: when crates migrated to 2018, `cargo fix --edition` rewrote identifiers named `async` to the raw identifier `r#async`. The edition guide goes as far as saying that a migration which is hard to do is a bug. ## How a crate picks its edition - The compiler takes the edition as a flag: `rustc --edition 2021 src/lib.rs`. Plain `rustc` with no flag uses **2015**, as `rustc --help` says ("The default is 2015 and the latest stable edition is 2024"). - Cargo passes that flag for you from the `edition` key of the package's manifest. When the key is missing, Cargo also falls back to **2015**, for backwards compatibility; how the key is written belongs to the material on `Cargo.toml`. - `cargo new` writes the newest stable edition, which on Cargo 1.99 is `edition = "2024"`. - The choice is **per crate**: every crate in a dependency graph has its own edition, and changing yours affects nobody else. ## What the compiler supports Every rustc release supports every edition that existed before it. Rust 1.99 accepts all four: | Edition | Stable since | Headline changes | |---|---|---| | 2015 | 1.0 | the original language | | 2018 | 1.31 | `async` and `await` become keywords, `dyn` a strict keyword, `try` reserved; the path and module-system changes (`crate::` paths, no `extern crate` needed) | | 2021 | 1.56 | disjoint closure captures, `array.into_iter()` by value, `TryFrom`/`TryInto`/`FromIterator` in the prelude, consistent `panic!` formatting, reserved prefixes such as `k#` | | 2024 | 1.85 | `gen` reserved, new temporary scopes, unsafe `extern` blocks and unsafe attributes, and more | New editions have arrived roughly every three years and ship through the normal six-week release train; the 2024 changes in detail have their own lesson. ## What goes into an edition, and what does not Only changes that would break existing code need an edition. Typical contents: 1. **New keywords**, which turn a valid identifier into a syntax error. 2. **Reserved syntax**, kept free for future features. 3. **Warnings promoted to errors**, such as a trait object written without `dyn`, a hard error since 2021. 4. **A different prelude**, the set of names imported into every module. Most other changes — new std types and methods, most new syntax, compiler and borrow-checker improvements — land on **all** editions at once. A 2015 crate built with rustc 1.99 gets the 1.99 standard library and diagnostics; it simply keeps the 2015 meaning of the words it already uses. ## An example: `async` as a name `let async = 3;` is a valid binding on the 2015 edition, where `async` is an ordinary identifier. Compiled with `--edition 2018` or later, rustc rejects it with "expected identifier, found keyword `async`" and suggests the **raw identifier** `r#async`, which is valid on every edition. That is why the 2015 crate keeps compiling on rustc 1.99: rustc compiles it as 2015 because that is the edition it asks for. ## Common confusions - An edition is **not a compiler version**: rustc 1.99 is one compiler that understands four editions. - An edition is **not** a fork of the language or of the standard library: crates on different editions link into one program, which is the next question to master. - Moving to a new edition is an **explicit migration**, usually with `cargo fix --edition`, never a side effect of `rustup update`.
- If most new features land on every edition, when do you actually have to change a crate's edition?When you want something that only exists in the newer mode: a keyword such as `async` (2018), the 2021 prelude, or features gated to 2024 such as let chains (stable since 1.88 on the 2024 edition only). New std APIs and most syntax arrive on every edition, so many crates stay on an older edition for years without missing much.
- What happens when you run plain `rustc main.rs` with no `--edition` flag on rustc 1.99?rustc compiles the file as the 2015 edition, its documented default. So `async fn` fails and `let async = 1;` compiles. Cargo always passes the flag, taking it from the manifest, which is why the difference rarely shows up outside one-off rustc invocations.
- Upgrading rustc moves every crate onto the newest edition.
- An edition is a separate compiler or a separate standard library.
- A missing edition key means the newest edition.
- New std APIs are available only on the newest edition.
- The edition is a setting for the whole dependency graph.
Assumes rustc and Cargo 1.99.1, where the editions are 2015, 2018 (1.31), 2021 (1.56) and 2024 (1.85), and cargo new writes edition 2024.