Skip to main content

compare_versions

Function compare_versions 

Source
pub fn compare_versions(a: &str, b: &str) -> Ordering
Expand description

Compares two Maven version strings by dot/dash-separated segment.

This is a total order (antisymmetric, transitive, and consistent with equality — verified by test_compare_versions_total_order_invariants), so it is safe to use as a sort_by comparator, e.g. sorting maven-metadata.xml’s version list in crate::registry::parse_metadata_xml. Each segment is classified as purely numeric (all ASCII digits) or a non-numeric qualifier. A numeric segment always outranks a non-numeric qualifier at the same position, which keeps legacy Maven identifiers such as Guava’s bare r03..r09 release tags below properly-formed numeric releases (e.g. 33.7.1-jre). A missing segment (the shorter version ran out of components) is ranked against a non-numeric qualifier at that position by the same Maven qualifier precedence used for two real qualifiers (see compare_qualifiers): a version’s own trailing dash-qualifier that ranks below release (e.g. -RC1, -SNAPSHOT) sorts below its base release (6.1.0-RC1 < 6.1.0), while one that ranks above release (e.g. -sp, or an unrecognized vendor suffix) sorts above it. Two numeric segments compare by magnitude (leading zeros ignored, no size limit); two real non-numeric segments are ranked by Maven qualifier precedence (see compare_qualifiers).

Note this does not treat a missing segment as equal to a present-but-zero one (1.0 and 1.0.0 compare unequal here) — doing so would break the total order, since the zero-valued segment and the missing one can each compare differently against a qualifier at that position depending on which side of the pair supplies it. Range/interval bound matching, which does want that normalization ([1.0] should match 1.0.0), uses the dedicated pairwise compare_versions_for_range instead.