LoginSignup
10
9

More than 5 years have passed since last update.

crate コンパイル時に crate のバージョン文字列を得る

Posted at

Cargo はコンパイル時に環境変数 CARGO_PKG_VERSION をセットする。これを env!() マクロで取得すればよい。

fn print_help() {
    println!("myapp version {}", env!("CARGO_PKG_VERSION"));
    // => myapp version 1.2.3-beta.1
}

コンパイル時に Cargo がセットする環境変数の一覧:

環境変数                    値の一例
--------------------------------------------
CARGO_MANIFEST_DIR         /path/to/my-crate
CARGO_PKG_VERSION          1.2.3-beta.1
CARGO_PKG_VERSION_MAJOR    1
CARGO_PKG_VERSION_MINOR    2
CARGO_PKG_VERSION_PATCH    3
CARGO_PKG_VERSION_PRE      beta.1

(参考: https://github.com/rust-lang/cargo/blob/0.3.0/src/cargo/ops/cargo_rustc/compilation.rs#L110-L116

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9