In Rust, why can a 2018-edition crate depend on 2021- and 2024-edition crates in one build, and what may an edition not change?
answer
- the edition is private to its crate
- skin-deep changes only
- one internal representation
- one std, one set of trait impls
basics
~20 sEach crate is compiled with its own edition, and every edition lowers to the same internal representation, so the compiled crates link normally. An edition may change keywords, syntax, lints and the prelude, but not std's types and trait impls.
solid answer
~40 srustc takes `--edition` per crate, and the edition only steers how that crate's own source is interpreted: lexing and keywords, some name and method resolution, lint levels, which prelude is imported. After that every crate lowers to the same internal representation and links against one std, so a 2018 crate calls a 2024 crate that uses let chains without knowing about them. That is the rule editions are built on: crates on different editions must interoperate, so an edition's choice is private. The price is that an edition cannot change what is shared: std's API, which trait impls exist, coherence, or how compiled crates talk to each other. When `IntoIterator` for arrays arrived it existed on every edition; only the `.into_iter()` method-call lookup differed by edition.
code
rust · 9 lines// scan: edition 2024, using a let chain (accepted on 2024 only)
pub fn first_even(items: &[Option<u32>]) -> Option<u32> {
for item in items {
if let Some(n) = item && n % 2 == 0 {
return Some(*n);
}
}
None
}go deeper
Recall that each crate has its own edition and that crates on different editions build into one program without any special setup.
Explain the mechanism: the edition is a per-crate compiler flag that only changes how that crate's source is read, after which everything shares one representation and one std.
Show the limits that follow: an edition cannot change trait impls, std's API or the interface between crates, and raw identifiers bridge keywords across the boundary.
Use the interoperability rule to argue that edition upgrades can be scheduled crate by crate, independently of dependencies, while watching exported macros and the minimum compiler version.
## The scenario A service's top-level crate is still on the **2018** edition. It depends on `ids`, a library on **2021**, and on `scan`, a library on **2024** that uses a let chain (`if let Some(n) = item && n % 2 == 0`), syntax that rustc accepts on 2024 only. The build succeeds and the program runs. Two facts make that work. ## Why it links 1. **The edition is a per-crate compiler setting.** rustc compiles one crate at a time and receives `--edition` for each one; Cargo fills it in from each package's own manifest. The 2018 crate never parses a line of `scan`'s source — it reads `scan`'s compiled metadata. 2. **Editions are skin deep.** The edition guide states the rule plainly: crates in one edition must seamlessly interoperate with crates compiled with other editions, and all Rust code, regardless of edition, compiles down to the same internal representation inside the compiler. An edition decides how source text is turned into that representation; it does not create a second kind of compiled crate. So the let chain inside `scan` is a fact about how `scan`'s source was parsed. To the 2018 caller, `scan::first_even` is just a function with a signature. ## Why the rule matters The interoperability rule is what keeps an edition from splitting the ecosystem. If a 2024 crate could only use 2024 dependencies, every library would have to migrate before any of its users could, and a library that upgraded early would lock out every user still on an older edition. Because the edition is private to each crate, none of that happens: - a library maintainer picks the edition that suits the code, on their own schedule; - an application can depend on libraries spread across all four editions; - nobody has to coordinate an edition change with anyone downstream or upstream. The cost of that freedom is the list of things an edition is not allowed to touch, below. ## Names cross the boundary, keywords do not Each crate is read with its own keywords. `ids` defines `pub fn gen(seed: u64) -> u64`, which is legal on 2021. The 2018 caller writes `ids::gen(5)` without trouble. A **2024** caller cannot: `gen` is a reserved keyword there, and rustc says "expected identifier, found reserved keyword `gen`" and suggests `ids::r#gen(5)`. The **raw identifier** syntax `r#` exists precisely so a newer crate can name an item a crate on an older edition was allowed to call `gen`, `async` or `dyn`. Macros follow the same rule from the other side: a `macro_rules!` macro's tokens keep the edition of the crate that defined it, wherever it expands. ## What an edition may change | May change | Examples | |---|---| | **Keywords and reserved syntax** | `async`/`await`/`dyn` (2018), prefixes like `k#` (2021), `gen` (2024) | | **How paths and names resolve** | `crate::` paths and no `extern crate` (2018), the 2021 prelude adding `TryFrom`, `TryInto`, `FromIterator` | | **Lint levels** | `bare_trait_objects` and `...` range patterns become errors in 2021 | | **Local meaning of code** | closures capturing fields instead of whole variables (2021), temporary scopes (2024) | | **Method-call lookup in narrow cases** | `array.into_iter()` (2021), `boxed_slice.into_iter()` (2024) | ## What an edition may not change - **The standard library's contents.** There is one `std` for the whole program. The *prelude* — which of its names are imported automatically — differs per edition (`std::prelude::rust_2018`, `rust_2021`, `rust_2024`), but the types, traits and methods are the same for every crate. - **Which trait impls exist.** A trait impl is a global fact checked by **coherence**, so it cannot exist for 2021 crates and not for 2018 crates in one program. The edition guide says this outright about arrays: since editions can be mixed, an impl cannot exist in one edition and not another, so `IntoIterator` for arrays was added on all editions in 1.53 and only the `.into_iter()` method-call syntax kept its old meaning before 2021. - **The interface between crates.** A function's signature, a type's layout and the calling convention between compiled crates do not depend on the edition of either side. ## Practical consequences - You can **upgrade your edition without waiting** for dependencies, and they can upgrade without waiting for you. - A library moving to a newer edition is not, by itself, a breaking change for callers, though it raises the minimum compiler version, and an exported macro can carry new-edition rules into callers. - One build can contain crates on all four editions at once, and large dependency graphs commonly do.
- Is moving a library from the 2021 to the 2024 edition a breaking change for the crates that depend on it?Not by itself: callers keep their own edition and see the same signatures. Two things can still bite them. The library now needs a compiler that knows the 2024 edition (1.85 or later), which matters for users on older toolchains. And an exported `macro_rules!` macro now expands with 2024 rules in the caller's crate, so test exported macros from older-edition crates before releasing.
- Why could the 2021 prelude add `TryInto` per edition when `IntoIterator` for arrays could not be per edition?The prelude is name resolution inside one crate: which std names are imported automatically into its modules. Each crate can import a different set without affecting anyone. A trait impl is a single global fact the whole program shares, so the impl was added on every edition and only the 2015/2018 method-call lookup was special-cased.
- How does a 2024 crate call a function named `gen` exported by a 2021 crate?With a raw identifier: `ids::r#gen(5)`. On 2024 `gen` is a reserved keyword, so the bare path fails with "expected identifier, found reserved keyword `gen`". The `r#` prefix tells the parser to treat the word as a plain name, which is how any crate refers to an item named after a keyword its own edition reserves.
Like a word processor that opens documents saved in an old file format and a new one: both become the same in-memory document, so you can paste between them. The format only changed how text is written on disk, which is why it cannot give one document a feature the other's in-memory copy lacks.
- A crate cannot depend on a crate that uses a newer edition.
- The whole dependency graph must be migrated to one edition together.
- An edition can add a trait impl only for crates on that edition.
- Each edition ships its own copy of the standard library.
- A 2018 caller cannot use a function whose body uses 2024-only syntax.
Assumes rustc 1.99 on the 2024 edition: let chains are stable since 1.88 on edition 2024 only, and gen is reserved from edition 2024.