Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 1.48 KB

embed-version-number-in-the-code.md

File metadata and controls

39 lines (28 loc) · 1.48 KB
title timestamp author published description tags todo
Embed version number in the binary compiled by Rust
2024-03-15 01:50:01 -0700
szabgab
true
We can bake the version number of the application in the code taking it from Cargo.toml.
version
CARGO_PKG_VERSION
env!
static
const

There are a number of environment variables available during the build of a binary. One of them is the CARGO_PKG_VERSION variable that contains the version number as written in the Cargo.toml file.

Using the env! macro we can extract the value during the build phase of the compilation and we can include it on our code either as a constant using the const keyword or we can make it a regular variable either explicitely making it a static variable or letting Rust figure out the type.

The difference will be the location where the value is stored.

The code

{% include file="examples/embed-version-number/src/main.rs" %}

Cargo.toml with the version number

{% include file="examples/embed-version-number/Cargo.toml" %}

Note

I'd go with the const.

If you only need to have a --version flag you can tell Clap to create a --version flag.