Skip to main content

Version

Struct Version 

Source
pub struct Version { /* private fields */ }
Expand description

表示一个版本号的结构体

包含了 major(主版本号) minor(次版本号) patch(补丁版本号) 和 可选的suffix(版本后缀)

use version::Version;

// 基于现有字符串
let version_s = Version::build_string("1.0.0").unwrap();
println!("{}", version_s.to_string())

Implementations§

Source§

impl Version

Source

pub fn build_string(version: &str) -> Result<Version, ParseError>

通过字符串构建 Version 结构体对象

§参数

version - 字符串必须遵循此结构 "XX.XX.XX-YY" 或者 "XX.XX-YY" 其中 YY 部分可缺省,此时的形式为 "XX.XX.XX"

§返回值

Ok(Version) - 版本号对象 Err(ParseError) - 解析错误

§示例
use version::Version;

let v = Version::build_string("1.0.0").unwrap();                // 主.副.补丁
let v_suffix = Version::build_string("2.0.0-beta").unwrap();    // 主.副.补丁-后缀
let v_major_minor = Version::build_string("1.2").unwrap();      // 主.副
Source

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

比较传入的版本号是否为最新版本

§参数
  • other - 传入要比较的版本号的地址
§返回值

true - 当传入版本号为最新时返回 false - 其他情况返回

§注意

判断是否为新版本逻辑如下

  1. 判断主版本号、副版本号、补丁版本号
  2. 判断两者之一是否有后缀,有后缀的版本号默认被认为是新版本
§示例
use version::Version;

let v_old = Version::build_string("1.0.0").unwrap();
let v_new = Version::build_string("2.0.0").unwrap();

assert_eq!(v_old.is_newer(&v_new), true)
Source

pub fn to_string(&self) -> String

将版本号转化为字符串。

§返回值

[major].[minor].[patch]-[suffix][major].[minor].[patch]形式输出

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.