Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away