Compiler Version Requirements
A package's compiler version requirement is declared in Cargo.toml using the rust-version field under [package] (or [workspace.package]). It specifies the oldest version of rustc and cargo that the package supports, enabling Cargo to fail fast with a clear diagnostic on older toolchains and—starting in the Rust 2024 edition (Resolver v3)—guiding dependency resolution automatically.
In Cargo.toml:
[package]
name = "my_library"
version = "0.4.0"
edition = "2024"
rust-version = "1.85" <--- Minimum Supported Compiler Version
|
+-- On `rustc 1.80`: Cargo immediately prints a clear error:
| `package my_library v0.4.0 cannot be built because it requires rustc 1.85 or newer`
|
+-- On `edition = "2024"` (Resolver v3):
`cargo add` and `cargo update` automatically prefer dependency versions
whose `rust-version` is `<= 1.85`!
Format and Rules for rust-version in Cargo.toml
- Can be written as a 2-component (
"1.85") or 3-component ("1.85.0") version number (without operators like^or>=). - Must be at least as new as the compiler version that introduced the package's declared
edition(for example,edition = "2024"requiresrust-version$\ge$"1.85"). - In a multi-crate workspace, define
rust-version = "1.85"once under[workspace.package]and inherit it in every member crate withrust-version.workspace = true.
# Workspace Root Cargo.toml:
[workspace]
resolver = "3"
members = ["crates/*"]
[workspace.package]
edition = "2024"
rust-version = "1.85"
# Member Crate Cargo.toml:
[package]
name = "core_protocol"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
MSRV-Aware Dependency Resolution in Rust 2024 (resolver = "3")
Prior to the Rust 2024 edition, if your crate declared rust-version = "1.80" and depended on foo = "1.0", and foo later published 1.5.0 requiring rust-version = "1.85", running cargo update would blindly select foo v1.5.0—breaking your build on rustc 1.80!
In Rust 2024 edition, edition = "2024" automatically enables Cargo Resolver v3 (resolver = "3"), which is MSRV-aware by default:
- When resolving dependencies, Cargo inspects your package's
rust-version(or the current runningrustcversion ifrust-versionis unset) and automatically selects the newest compatible dependency version (foo v1.4.2) whoserust-versionis $\le$ your requirement!
| Resolver Version | Default In | Behavior When foo v1.5.0 Requires Newer rustc Than Your rust-version |
|---|---|---|
resolver = "2" |
edition = "2021" |
Selects foo v1.5.0 anyway, causing compilation to fail on your MSRV toolchain. |
resolver = "3" |
edition = "2024" |
Automatically selects foo v1.4.x (compatible with your rust-version) unless no compatible version exists! |
fn main() {
let declared_msrv = option_env!("CARGO_PKG_RUST_VERSION").unwrap_or("1.85");
println!("Package declared rust-version: {declared_msrv}");
}
Package declared rust-version: 1.85