Struct Version

pub struct Version { pub major: u64, pub minor: u64, pub patch: u64, pub pre: Prerelease, pub build: BuildMetadata }

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, 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(),
]);

Trait Implementations

impl Clone for Version

fn clone(&self) -> Version

impl Debug for Version

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

impl Display for Version

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

impl Eq for Version

impl FromStr for Version

type Err = Error;
fn from_str(text: &str) -> Result<Self, Self::Err>

impl Hash for Version

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

impl Ord for Version

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

impl PartialEq for Version

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

impl PartialOrd for Version

fn partial_cmp(&self, other: &Version) -> Option<Ordering>

impl Serialize for Version

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

impl StructuralPartialEq for Version

impl<'de> Deserialize<'de> for Version

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

Auto Trait Implementations

impl Freeze for Version

impl RefUnwindSafe for Version

impl Send for Version

impl Sync for Version

impl Unpin for Version

impl UnsafeUnpin for Version

impl UnwindSafe for Version

Blanket Implementations

impl<T> Any for Version where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Version where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Version where T: ?Sized,

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

impl<T> CloneToUninit for Version where T: Clone,

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

impl<T> DeserializeOwned for Version where T: for<'de> Deserialize<'de>,

impl<T> From<T> for Version

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Version where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for Version where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for Version where U: From<T>,

fn into(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<U> for Version where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Version where U: TryFrom<T>,

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