Skip to main content

Crate rustversion_detect

Crate rustversion_detect 

Source
Expand description

Parses the output of rustc --version for use in build scripts.

The parsed output is cached in a static variable, so rustc will only be invoked once no matter how many build scripts use this crate. This gives an advantage over using autocfg crate or performing ad-hoc detection, as these require re-running rustc for each build scripts.

This crate originated as a fork of dtolnay’s rustversion crate, but with the proc-macro code removed and version detection moved from the build script to runtime. The core version detection logic has been kept up to date with upstream changes. It currently mirrors rustversion v1.0.23.

Moving the version detection logic to runtime means that this crate does not need its own build script, reducing compile times compared to using the rustversion macros in a build script.

§Dependency

Add the following to your build script:

[build-dependencies]
rustversion-detect = "0.2"

§Examples

pub fn main() {
    // by default rust re-runs the build script if any source file changes
    // this directive indicates that the build script only needs
    // to be rerun if the compiler flags change (or if the build script itself changes)
    println!("cargo:rerun-if-changed=build.rs");

    // Rust 1.80 requires listing all possibilities using this directive
    println!("cargo:rustc-check-cfg=cfg(use_nightly)");

    let version = rustversion_detect::detect_version().unwrap();
    if version.is_nightly() {
        println!("cargo:rustc-cfg=use_nightly");
    }
}

Re-exports§

pub use crate::date::Date;
pub use crate::version::Channel;
pub use crate::version::RustVersion;
pub use crate::version::StableVersionSpec;

Modules§

date
Contains a basic Date type used for differentiating nightly versions of rust.
version
Defines the rust version types.

Structs§

VersionDetectionError
Indicates failure to detect the compiler’s rust version.

Functions§

detect_version
Detect the current version by executing rustc.