Sub topic 11
Which Rust version and edition you need
This chapter assumes you can read if let, while let, and let else, and that you know what a Cargo project and Cargo.toml are. Most of these constructs work on any modern compiler. Let chains are the exception, because they need both a recent compiler and a specific edition.
Version and edition are different things
A Rust version is the compiler release you have installed, such as 1.88. An edition is a language setting chosen per crate in Cargo.toml. Editions exist so Rust can make small changes to the language without breaking old code. The editions are 2015, 2018, 2021, and 2024.
Check your compiler with:
rustc --version
cargo --versionUpdate it with rustup update stable.
What each construct requires
| Construct | First stable in | Edition needed |
|---|---|---|
| `if let`, `while let` | Rust 1.0 era (`while let` since 1.0, `if let` since 1.0) | Any |
| `let else` | Rust 1.65 | Any |
| Edition 2024 itself | Rust 1.85 | 2024 |
| let chains | Rust 1.88 | 2024 only |
A let chain is a condition that joins let patterns and boolean tests with &&. It needs both columns of the table: a compiler of 1.88 or newer, and edition = "2024". Compiler 1.88 with an older edition still rejects let chains.
Setting the edition
The edition lives in the [package] section of Cargo.toml:
[package]
name = "chains-demo"
version = "0.1.0"
edition = "2024"
rust-version = "1.88"The optional rust-version field states the oldest compiler your crate supports. With it set, Cargo stops with a clear message on an older compiler, so users see a version error instead of a confusing syntax error.
New projects made with cargo new on Rust 1.85 or newer default to edition 2024. A project created earlier probably says 2021, and you must change it by hand.
A program that needs both
Put this in src/main.rs of the project above:
fn first_positive(items: &[&str]) -> Option<i32> {
let first = items.first()?;
if let Ok(n) = first.parse::<i32>()
&& n > 0
{
Some(n)
} else {
None
}
}
fn main() {
println!("{:?}", first_positive(&["7", "x"]));
println!("{:?}", first_positive(&["-3"]));
println!("{:?}", first_positive(&[]));
}It prints Some(7), None, and None.
Recognizing the errors
If the edition is too old, the compiler reports that let chains are only allowed in Rust 2024 or later. The fix is the edition line, not the code.
If the compiler is too old, the same code fails with an error saying let expressions in this position are unstable. The fix is rustup update.
The two errors look alike, so check both settings before changing code.
What let chains do not allow
Even with the right setup, a chain only works with &&. Mixing let with || is a compile error. Let chains also work in while conditions, not only if.
Exercise
Your project has edition = "2021" and you install Rust 1.90. You write if let Some(x) = a && x > 0 { ... }. Does it compile? What must you change?
Show answer
No. The compiler is new enough (1.88 or newer), but let chains need edition 2024. Change edition to "2024" in Cargo.toml, preferably after running cargo fix --edition, since the new edition changes some temporary-drop behavior.
Exercise
Which of if let, let else, and let chains would still compile on Rust 1.70 with edition 2021?
Show answer
if let and let else. let else has been stable since 1.65. Let chains need Rust 1.88 and edition 2024.