Struct version_compare::Version[][src]

pub struct Version<'a> { /* fields omitted */ }
Expand description

Version struct, wrapping a string, providing useful comparison functions.

A version in string format can be parsed using methods like Version::from("1.2.3");. These methods return a Result holding the parsed version or an error on failure.

The original version string is stored in the struct, and can be accessed using the version.as_str() method. Note, that when the version wasn’t parsed from a string representation, the returned value is generated.

The struct provides many methods for comparison and probing.

Implementations

Create a Version instance from a version string.

The version string should be passed to the version parameter.

Examples
use version_compare::{Cmp, Version};

let ver = Version::from("1.2.3").unwrap();

assert_eq!(ver.compare(Version::from("1.2.3").unwrap()), Cmp::Eq);

Create a Version instance from already existing parts

Examples
use version_compare::{Cmp, Version, Manifest};

Create a Version instance from a version string with the given manifest.

The version string should be passed to the version parameter.

Examples
use version_compare::{Cmp, Version, Manifest};

let manifest = Manifest::default();
let ver = Version::from_manifest("1.2.3", &manifest).unwrap();

assert_eq!(ver.compare(Version::from("1.2.3").unwrap()), Cmp::Eq);

Get the version manifest, if available.

Examples
use version_compare::Version;

let version = Version::from("1.2.3").unwrap();

if version.has_manifest() {
    println!(
        "Maximum version part depth is {} for this version",
        version.manifest().unwrap().max_depth.unwrap_or(0),
    );
} else {
    println!("Version has no manifest");
}

Check whether this version has a manifest.

Examples
use version_compare::Version;

let version = Version::from("1.2.3").unwrap();

if version.has_manifest() {
    println!("This version does have a manifest");
} else {
    println!("This version does not have a manifest");
}

Set the version manifest.

Examples
use version_compare::{Version, Manifest};

let manifest = Manifest::default();
let mut version = Version::from("1.2.3").unwrap();

version.set_manifest(Some(&manifest));

Get the original version string.

Examples
use version_compare::Version;

let ver = Version::from("1.2.3").unwrap();

assert_eq!(ver.as_str(), "1.2.3");

Get a specific version part by it’s index. An error is returned if the given index is out of bound.

Examples
use version_compare::{Version, Part};

let ver = Version::from("1.2.3").unwrap();

assert_eq!(ver.part(0), Ok(Part::Number(1)));
assert_eq!(ver.part(1), Ok(Part::Number(2)));
assert_eq!(ver.part(2), Ok(Part::Number(3)));

Get a vector of all version parts.

Examples
use version_compare::{Version, Part};

let ver = Version::from("1.2.3").unwrap();

assert_eq!(ver.parts(), &vec![
    Part::Number(1),
    Part::Number(2),
    Part::Number(3)
]);

Compare this version to the given other version.

This method returns one of the following comparison operators:

  • Lt
  • Eq
  • Gt

Other comparison operators can be used when comparing, but aren’t returned by this method.

Examples:
use version_compare::{Cmp, Version};

assert_eq!(Version::from("1.2").unwrap().compare(Version::from("1.3.2").unwrap()), Cmp::Lt);
assert_eq!(Version::from("1.9").unwrap().compare(Version::from("1.9").unwrap()), Cmp::Eq);
assert_eq!(Version::from("0.3.0.0").unwrap().compare(Version::from("0.3").unwrap()), Cmp::Eq);
assert_eq!(Version::from("2").unwrap().compare(Version::from("1.7.3").unwrap()), Cmp::Gt);

Compare this version to the given other version, and check whether the given comparison operator is valid.

All comparison operators can be used.

Examples:
use version_compare::{Cmp, Version};

assert!(Version::from("1.2").unwrap().compare_to(Version::from("1.3.2").unwrap(), Cmp::Lt));
assert!(Version::from("1.2").unwrap().compare_to(Version::from("1.3.2").unwrap(), Cmp::Le));
assert!(Version::from("1.2").unwrap().compare_to(Version::from("1.2").unwrap(), Cmp::Eq));
assert!(Version::from("1.2").unwrap().compare_to(Version::from("1.2").unwrap(), Cmp::Le));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Implement the partial equality trait for the version struct, to easily allow version comparison.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Implement the partial ordering trait for the version struct, to easily allow version comparison.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.