Go to file
Josh Stone cb8592005c Parse version and date from '--verbose'.
The plain `rustc --version` string is not well structured, and in
particular the git commit info is not necessarily present when rustc is
built out of tree, such as in distro builds. Furthermore,
rust-lang/rust#79115 made it possible to have completely custom
information in a version's parenthesized block, which may not look like
git info at all.

Adding `--verbose` outputs each field on its own line, in particular
"release: ..." for the version number and "commit-date: ..." for the git
info, although the latter is just "unknown" for out-of-tree builds.

MSRV remains 1.0.0.
2021-03-16 00:58:09 -07:00
src Parse version and date from '--verbose'. 2021-03-16 00:58:09 -07:00
.gitignore Initial commit. 2017-01-15 00:46:39 -08:00
.travis.yml New version: 0.9. 2019-05-23 19:11:55 -07:00
Cargo.toml New version: 0.9.2. 2020-05-23 21:24:30 -07:00
LICENSE-APACHE Relicense under dual MIT/Apache-2. 2018-07-05 12:49:43 -07:00
LICENSE-MIT Relicense under dual MIT/Apache-2. 2018-07-05 12:49:43 -07:00
README.md New version: 0.9. 2019-05-23 19:11:55 -07:00

version_check

Build Status Current Crates.io Version rustdocs on docs.rs

This tiny crate checks that the running or installed rustc meets some version requirements. The version is queried by calling the Rust compiler with --version. The path to the compiler is determined first via the RUSTC environment variable. If it is not set, then rustc is used. If that fails, no determination is made, and calls return None.

Usage

Add to your Cargo.toml file, typically as a build dependency:

[build-dependencies]
version_check = "0.9"

version_check is compatible and compiles with Rust 1.0.0 and beyond.

Examples

Set a cfg flag in build.rs if the running compiler was determined to be at least version 1.13.0:

extern crate version_check as rustc;

if rustc::is_min_version("1.13.0").unwrap_or(false) {
    println!("cargo:rustc-cfg=question_mark_operator");
}

Check that the running compiler was released on or after 2018-12-18:

extern crate version_check as rustc;

match rustc::is_min_date("2018-12-18") {
    Some(true) => "Yep! It's recent!",
    Some(false) => "No, it's older.",
    None => "Couldn't determine the rustc version."
};

Check that the running compiler supports feature flags:

extern crate version_check as rustc;

match rustc::is_feature_flaggable() {
    Some(true) => "Yes! It's a dev or nightly release!",
    Some(false) => "No, it's stable or beta.",
    None => "Couldn't determine the rustc version."
};

See the rustdocs for more examples and complete documentation.

Alternatives

This crate is dead simple with no dependencies. If you need something more and don't care about panicking if the version cannot be obtained, or if you don't mind adding dependencies, see rustc_version.

License

version_check is licensed under either of the following, at your option: