Sub topic 11
Which Rust version and edition you need
This chapter assumes you can already read if let, while let, let else, and let chains, and that you can run cargo build. It covers what each construct needs from your toolchain, what the errors look like when you don't have it, and how to upgrade safely.
Two separate settings: compiler version and edition
Two settings decide whether a piece of syntax compiles.
- Compiler version is the release of
rustcyou run, such as 1.88.0. New releases ship every six weeks. A language feature is available once a release stabilizes it. - Edition is a per-crate setting in
Cargo.toml, such as2018,2021, or2024. An edition is an opt-in bundle of changes that could break existing code, so they cannot be switched on for everyone at once.
Most features need only a recent enough compiler and work in every edition. A few need both a minimum compiler version and a minimum edition. Let chains are in the second group.
Version and edition requirements by construct
| Construct | Stable since | Edition needed |
|---|---|---|
| `if let`, `if let ... else` | Rust 1.0 | Any |
| `while let` | Rust 1.0 | Any |
| `matches!` | Rust 1.42 | Any |
| `let else` | Rust 1.65 | Any |
| let chains (`if let ... && ...`) | Rust 1.88 | 2024 only |
| `if let` temporaries dropped before `else` | Rust 1.85 | 2024 only |
Edition 2024 itself was stabilized in Rust 1.85, so a 1.88 compiler is always new enough to support it. The reverse does not hold. A 1.88 compiler building a crate that says edition = "2021" still rejects let chains.
Checking what you have
Print the compiler and Cargo versions:
rustc --version
cargo --versionIf the version is older than 1.88, update the stable toolchain:
rustup update stableYour crate's edition is in Cargo.toml:
[package]
name = "chains-demo"
version = "0.1.0"
edition = "2024"
rust-version = "1.88"Two keys matter here.
editionselects the language rules for this crate. If you leave it out, Cargo assumes2015, which surprises many people with older manifests.rust-versiondeclares the minimum compiler your crate supports. It is optional. Cargo checks it and fails early with a message naming the required version, so a user on an old toolchain sees "requires rustc 1.88" and not a syntax error deep in your code.
cargo new writes the edition of the toolchain you run it with. A project created with Rust 1.85 or newer therefore starts at 2024. A project created earlier may be on 2021 and must be moved by hand.
A program that needs both
This is the let chain example from earlier chapters. It compiles only when both requirements hold.
// Requires Rust 1.88 or newer and edition = "2024" in Cargo.toml.
fn describe(first: Option<&str>) -> String {
if let Some(text) = first
&& let Ok(n) = text.parse::<i32>()
&& n > 0
{
format!("positive number {n}")
} else {
String::from("no positive number")
}
}
fn main() {
println!("{}", describe(Some("42")));
println!("{}", describe(Some("-1")));
println!("{}", describe(None));
}Let chains also work in while:
// Requires Rust 1.88 or newer and edition = "2024" in Cargo.toml.
fn main() {
let mut stack = vec![5, 3, 0, 8];
while let Some(top) = stack.pop()
&& top != 0
{
println!("popped {top}");
}
println!("left on the stack: {stack:?}");
}The loop pops 8, sees a non-zero value, and prints it. It then pops 0 and stops. The 0 was already removed from the stack, so stack still holds [5, 3]. Order matters in a chain: the pop happens before the test, so a value that fails the test is consumed.
Reading the two failure modes
You can fail the requirement in two ways. The error tells you which one.
Right compiler, wrong edition
Suppose you have Rust 1.88 or newer, but Cargo.toml says edition = "2021". The compiler recognizes the syntax and refuses it for this edition. The message is similar to:
error: let chains are only allowed in Rust 2024 or laterThe fix is to change the edition, not the compiler. Updating Rust again does nothing.
Right edition, old compiler
Suppose edition = "2024" is set, but your compiler is older than 1.88. The compiler treats the syntax as an unstable feature. On the older releases that already understood edition 2024 (1.85 to 1.87), you see an error similar to:
error[E0658]: `let` expressions in this position are unstableThe fix here is rustup update. Before 1.85, the compiler cannot read edition = "2024" at all, and Cargo reports that the edition is unknown or unstable. Treat the exact wording as approximate, since it changes between releases. The cue to look for is a mention of "unstable" or "only allowed in Rust 2024".
Why let chains are tied to an edition
The compiler could have enabled let chains everywhere once it understood them. They are tied to edition 2024 because of how if let handles temporary values, which are values the compiler creates while evaluating an expression and drops at some later point.
Edition 2024 changed when temporaries in an if let condition are dropped. Let chains rely on the new rules, so the two changes ship together. For you as a user, the practical result is that you cannot adopt let chains without also adopting the new temporary rules.
What else changes with edition 2024
When you move a crate from 2021 to 2024, you accept the whole bundle, not just let chains. Two changes interact with the constructs in this tutorial.
`if let` temporaries drop before `else`
In edition 2021, a temporary created in the scrutinee (the expression after =) of if let lives until the end of the whole if let ... else statement. That includes the else block. In 2024, the temporary is dropped before the else block runs.
struct Noisy(&'static str);
impl Drop for Noisy {
fn drop(&mut self) {
println!("drop {}", self.0);
}
}
impl Noisy {
fn get(&self) -> Option<u8> {
None
}
}
fn main() {
if let Some(n) = Noisy("temp").get() {
println!("some {n}");
} else {
println!("in else");
}
}The output depends on the edition.
| Edition | Output order |
|---|---|
| 2021 | `in else`, then `drop temp` |
| 2024 | `drop temp`, then `in else` |
This matters when the temporary holds a lock or a borrow. In 2021, code such as if let Some(x) = mutex.lock().unwrap().get() { ... } else { mutex.lock() ... } could deadlock in the else branch, because the first guard was still alive. In 2024 the guard is gone by then, so that code no longer deadlocks.
Tail expression temporaries
Edition 2024 also drops temporaries in a block's final expression earlier. It is a separate change, but it often appears in the same migration report.
The temporaries topic has its own chapter later in the series. For now, the point is only that migrating has behavioral effects beyond the new syntax.
Moving a crate to edition 2024
Follow these steps in order.
- Update the toolchain with
rustup update stableand confirmrustc --versionshows 1.88 or newer if you want let chains. - Commit your current work so the migration diff stands alone.
- Run the automatic migration:
``bash cargo fix --edition ``
This rewrites code that would change meaning, and it leaves warnings for the cases it cannot fix.
- Set
edition = "2024"inCargo.toml. Optionally addrust-version = "1.88". - Run
cargo buildandcargo test. Fix anything the migration flagged. - Rewrite nested
if letblocks as let chains where it improves readability.
Step 3 must come before step 4. The migration tool compares your code under the old edition with the new rules, so it needs to run while the manifest still says 2021.
Workspaces and dependencies
An edition belongs to one crate. Your crate on 2024 can depend on a library that is still on 2018 or 2021, and the compiler handles each crate under its own rules. You do not have to wait for your dependencies to migrate.
In a workspace, each member declares its own edition unless you share it. To keep them aligned, put the value in the workspace root and inherit it:
# Root Cargo.toml
[workspace]
members = ["app", "util"]
[workspace.package]
edition = "2024"# app/Cargo.toml
[package]
name = "app"
version = "0.1.0"
edition.workspace = trueMembers can migrate one at a time if you set edition per crate instead.
Supporting older compilers
If you publish a library, raising its edition to 2024 raises its minimum compiler to at least 1.85. Using let chains raises it to 1.88. Both choices exclude users on older toolchains. Weigh these points:
let elseworks from 1.65 in any edition. If you must support older compilers, it removes much of the nesting without needing 2024.- Declare
rust-versionso the requirement is explicit and users get a clear message. - Raising the minimum version is a normal change for applications. For widely used libraries, many teams treat it as a notable change and note it in the changelog.
Summary
if let,while let, andmatches!have been stable for years and need nothing special.let elseneeds Rust 1.65 or newer, in any edition.- Let chains need Rust 1.88 or newer and edition 2024. Both conditions must hold.
- Edition 2024 also changes temporary lifetimes in
if let, so migrate withcargo fix --editionand run your tests.
Exercise
A teammate on Rust 1.90 adds a let chain to a crate whose Cargo.toml says edition = "2021". The build fails. Which setting do they change, and why does running rustup update not help?
Show answer
They change edition to "2024" (after running cargo fix --edition). The compiler is already new enough, since let chains need 1.88 or newer, but the feature is only allowed in edition 2024. Updating the toolchain cannot change a crate's edition.
Exercise
You must keep supporting Rust 1.70 and want to flatten a nested "unwrap or return early" pattern. Which construct can you use, and which can't you?
Show answer
Use let else, which is stable since 1.65 and works in every edition. Let chains are out, because they need Rust 1.88 and edition 2024.
Exercise
In edition 2021, if let Some(n) = Noisy("temp").get() { ... } else { println!("in else"); } prints "in else" before "drop temp". What does edition 2024 print, and what is the practical benefit?
Show answer
Edition 2024 prints "drop temp" first, then "in else", because the scrutinee's temporaries are dropped before the else block runs. The benefit is that guards or borrows held by those temporaries, such as a mutex guard, are released before the else branch, which avoids deadlocks and borrow conflicts there.