Compare Sequences Lexicographically With cmp
Order two sequences element by element with Iterator::cmp: first difference decides, equal prefixes leave the shorter one smaller, is_lt reads as a boolean.
Code
fn main() {
// version vectors: compare element by element, the first difference decides the order (lexicographic)
let deployed: [u32; 3] = [1, 4, 2];
let candidate: [u32; 3] = [1, 5, 0];
// iterator cmp returns an Ordering; the three outcomes are distinct variants
match deployed.iter().cmp(candidate.iter()) {
std::cmp::Ordering::Less => println!("candidate is newer"),
std::cmp::Ordering::Equal => println!("identical"),
std::cmp::Ordering::Greater => println!("candidate is older"),
}
// boolean wrappers over the same comparison: is_lt / is_ge without a match
let older: [u32; 3] = [1, 2, 9];
println!("older < deployed: {}", older.iter().cmp(deployed.iter()).is_lt());
println!("deployed >= deployed: {}", deployed.iter().cmp(deployed.iter()).is_ge());
// different lengths: with a shared prefix, the shorter one is smaller
let short: [u32; 2] = [1, 4];
println!("short prefix < deployed: {}", short.iter().cmp(deployed.iter()).is_lt());
}Inside the snippet
Count the function main as the entire surface area of Compare Sequences Lexicographically With cmp.
The match covers the value’s shapes one arm at a time — nothing falls to a default by accident.
Putting it to use
Running the file prints labeled results such as candidate is newer, identical, candidate is older — each operation’s effect is visible in the output.
Core lines
match deployed.iter().cmp(candidate.iter()) {
std::cmp::Ordering::Less => println!("candidate is newer"),
std::cmp::Ordering::Equal => println!("identical"),
std::cmp::Ordering::Greater => println!("candidate is older"),
}
Interactive Tools for This Topic
SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Text Diff ComparisonCompare two text blocks line by line and visualize the differences with added, removed, and unchanged lines highlighted. Useful for reviewing changes and patches.
FAQ
- What does Compare Sequences Lexicographically With cmp print when run?
- Running the file directly is enough — expect labeled output lines such as `candidate is newer`, `identical`, `candidate is older`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.