Struct Version

struct Version { ... }

SemVer version as defined by https://semver.org.

Syntax

Total ordering

Given any two SemVer versions, one is less than, greater than, or equal to the other. Versions may be compared against one another using Rust's usual comparison operators.

Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0

Fields

major: u64
minor: u64
patch: u64
pre: Prerelease
build: BuildMetadata

Implementations

impl Version

const fn new(major: u64, minor: u64, patch: u64) -> Self

Create Version with an empty pre-release and build metadata.

Equivalent to:

# use semver::{BuildMetadata, Prerelease, Version};
#
# const fn new(major: u64, minor: u64, patch: u64) -> Version {
Version {
    major,
    minor,
    patch,
    pre: Prerelease::EMPTY,
    build: BuildMetadata::EMPTY,
}
# }
fn parse(text: &str) -> Result<Self, Error>

Create Version by parsing from string representation.

Errors

Possible reasons for the parse to fail include:

  • 1.0 — too few numeric components. A SemVer version must have exactly three. If you are looking at something that has fewer than three numbers in it, it's possible it is a VersionReq instead (with an implicit default ^ comparison operator).

  • 1.0.01 — a numeric component has a leading zero.

  • 1.0.unknown — unexpected character in one of the components.

  • 1.0.0- or 1.0.0+ — the pre-release or build metadata are indicated present but empty.

  • 1.0.0-alpha_123 — pre-release or build metadata have something outside the allowed characters, which are 0-9, A-Z, a-z, -, and . (dot).

  • 23456789999999999999.0.0 — overflow of a u64.

fn cmp_precedence(self: &Self, other: &Self) -> Ordering

Compare the major, minor, patch, and pre-release value of two versions, disregarding build metadata. Versions that differ only in build metadata are considered equal. This comparison is what the SemVer spec refers to as "precedence".

Example

use semver::Version;

let mut versions = [
    "1.20.0+c144a98".parse::<Version>().unwrap(),
    "1.20.0".parse().unwrap(),
    "1.0.0".parse().unwrap(),
    "1.0.0-alpha".parse().unwrap(),
    "1.20.0+bc17664".parse().unwrap(),
];

// This is a stable sort, so it preserves the relative order of equal
// elements. The three 1.20.0 versions differ only in build metadata so
// they are not reordered relative to one another.
versions.sort_by(Version::cmp_precedence);
assert_eq!(versions, [
    "1.0.0-alpha".parse().unwrap(),
    "1.0.0".parse().unwrap(),
    "1.20.0+c144a98".parse().unwrap(),
    "1.20.0".parse().unwrap(),
    "1.20.0+bc17664".parse().unwrap(),
]);

// Totally order the versions, including comparing the build metadata.
versions.sort();
assert_eq!(versions, [
    "1.0.0-alpha".parse().unwrap(),
    "1.0.0".parse().unwrap(),
    "1.20.0".parse().unwrap(),
    "1.20.0+bc17664".parse().unwrap(),
    "1.20.0+c144a98".parse().unwrap(),
]);

impl Clone for Version

fn clone(self: &Self) -> Version

impl Debug for crate::Version

fn fmt(self: &Self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result

impl Display for crate::Version

fn fmt(self: &Self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result

impl Eq for Version

impl Freeze for Version

impl FromStr for crate::Version

fn from_str(text: &str) -> Result<Self, <Self as >::Err>

impl Hash for Version

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl Ord for Version

fn cmp(self: &Self, other: &Version) -> $crate::cmp::Ordering

impl PartialEq for Version

fn eq(self: &Self, other: &Version) -> bool

impl PartialOrd for Version

fn partial_cmp(self: &Self, other: &Version) -> $crate::option::Option<$crate::cmp::Ordering>

impl RefUnwindSafe for Version

impl Send for Version

impl Serialize for crate::Version

fn serialize<S>(self: &Self, serializer: S) -> Result<<S as >::Ok, <S as >::Error>
where
    S: Serializer

impl StructuralPartialEq for Version

impl Sync for Version

impl Unpin for Version

impl UnwindSafe for Version

impl<'de> Deserialize for crate::Version

fn deserialize<D>(deserializer: D) -> Result<Self, <D as >::Error>
where
    D: Deserializer<'de>

impl<T> Any for Version

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Version

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Version

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Version

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> DeserializeOwned for Version

impl<T> From for Version

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Version

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for Version

fn to_string(self: &Self) -> String

impl<T, U> Into for Version

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Version

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Version

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>