Embed time of compilation and other build-time information in a rust binary

build

Sometimes it is useful to be able to show information about the compilation of a rust crate.

For example the date and time when it was compiled.

Cargo.toml

We need to add the built crate as a build-time dependency.

examples/embed-build-time-data/Cargo.toml

[package]
name = "embed-build-time-data"
version = "0.1.0"
edition = "2024"

[dependencies]

[build-dependencies]
built = { version = "0.8", features = ["cargo-lock", "dependency-tree", "chrono", "git2", "semver"] }


build.rs

We need to create build.rs in the root of the crate.

examples/embed-build-time-data/build.rs

fn main() {
    built::write_built_file().expect("Failed to acquire build-time information")
}

main.rs

We can then access the data in our code during run-time.

examples/embed-build-time-data/src/main.rs

mod built_info {
    include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

fn main() {
    println!("TARGET                {:?}", built_info::TARGET);
    println!("PKG_VERSION           {:?}", built_info::PKG_VERSION);
    println!("RUSTC_VERSION         {:?}", built_info::RUSTC_VERSION);

    println!("GIT_VERSION           {:?}", built_info::GIT_VERSION);
    println!("GIT_DIRTY             {:?}", built_info::GIT_DIRTY);
    println!("GIT_COMMIT_HASH       {:?}", built_info::GIT_COMMIT_HASH);
    println!(
        "GIT_COMMIT_HASH_SHORT {:?}",
        built_info::GIT_COMMIT_HASH_SHORT
    );

    println!("BUILT_TIME_UTC        {:?}", built_info::BUILT_TIME_UTC);

    println!("CI_PLATFORM           {:?}", built_info::CI_PLATFORM);
}

Output

$cargo run

TARGET                "x86_64-unknown-linux-gnu"
PKG_VERSION           "0.1.0"
RUSTC_VERSION         "rustc 1.93.0 (254b59607 2026-01-19)"
GIT_VERSION           Some("9c6447a")
GIT_DIRTY             Some(true)
GIT_COMMIT_HASH       Some("9c6447a01046142ccf471a597f21d2c08c67c2cd")
GIT_COMMIT_HASH_SHORT Some("9c6447a")
BUILT_TIME_UTC        "Fri, 6 Feb 2026 10:42:50 +0000"
CI_PLATFORM           None

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo