Versions
- Rust course
- Intermediate
- Lesson 79/115
- 4 min
Learn semver version specifiers and the cargo update command.
Versions
Crates use semantic versioning (semver): MAJOR.MINOR.PATCH. An incompatible change bumps MAJOR, a backward-compatible new feature MINOR, a bug fix PATCH. "1.2.3" in Cargo.toml actually means ^1.2.3:
"1.2.3" = ^1.2.3 → >=1.2.3, <2.0.0
"0.8" = ^0.8 → >=0.8.0, <0.9.0 (in 0.x, MINOR is breaking)
"~1.2" → >=1.2.0, <1.3.0
"=1.2.3" → only 1.2.3Tuple comparison
In Rust, tuples compare lexicographically: first elements first, then the seconds if equal... This makes comparing versions easy:
println!("{}", (1, 4, 0) > (1, 2, 3)); // true
println!("{}", (2, 0, 0) > (1, 9, 9)); // true
// the ^required rule (for MAJOR > 0)
fn uyumlu(gerekli: (u32, u32, u32), mevcut: (u32, u32, u32)) -> bool {
mevcut.0 == gerekli.0 && mevcut >= gerekli
}Practice Compatibility check
Write the uyumlu function above. Print the results for versions (1, 4, 0), (2, 0, 0) and (1, 2, 1) against the requirement (1, 2, 3), separated by spaces.
Starter code
fn main() {
// buraya yaz
}Sample solution
fn uyumlu(gerekli: (u32, u32, u32), mevcut: (u32, u32, u32)) -> bool {
mevcut.0 == gerekli.0 && mevcut >= gerekli
}
fn main() {
let g = (1, 2, 3);
println!("{} {} {}", uyumlu(g, (1, 4, 0)), uyumlu(g, (2, 0, 0)), uyumlu(g, (1, 2, 1)));
}Expected output
true false false
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Does the dependency <code>rand = "0.8"</code> pick up version 0.9.0 automatically?
- Only with cargo update
- Only in release
- No; in 0.x versions a MINOR bump counts as breaking Correct answer
- Yes
Explanation: Before 1.0 the API is considered unstable, so the caret rule works on the first non-zero component.
2 Exact version
serde = "____1.0.193" # yalnızca bu sürüm
serde = "=1.0.193" # yalnızca bu sürüm
Explanation: The = prefix locks to a single version.
3 Requirement and allowed range
| Term | Meaning |
|---|---|
"1.2.3" |
>=1.2.3, <2.0.0 |
"0.8" |
>=0.8.0, <0.9.0 |
"~1.2" |
>=1.2.0, <1.3.0 |
"=1.2.3" |
Only 1.2.3 |