Clap - show version number of the command line application in Rust

clap command version --version -V

It is usually quite useful for command line applications to be able to tell which version they are.

The common way is that they have a command line flag --version and sometimes -V does the same.

Just try

rustup --version

rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.74.1 (a28077b28 2023-12-04)`


rustup -V

It is very easy to do this using Clap, we only need to add the following to the struct that defines the parameters:


Clap will automatically add a --version and the -V flags to the application and when the user runs the cli tool providing either of those flags it will print the name of the cli tool and the version number taken from Cargo.toml.

Both values coming from the Cargo.toml file.


To demonstrate I created a Crate called show-version and added the Clap crate as a dependency including the derive feature:

cargo add clap --features derive

The Cargo.toml looks like this:


name = "show-version"
version = "1.2.3"
edition = "2021"

# See more keys and their definitions at

clap = { version = "4.4.11", features = ["derive"] }

Full Code


use clap::Parser;

struct Cli {
    host: String,

fn main() {
    let args = Cli::parse();

cargo run -- --version
show-version 1.2.3

As we saw in the article about getting started with command line parsing in Rust we can also run the compiled code directly and then we don't need the -- separator:

$ ./target/debug/show-version -V
show-version 1.2.3

If we move and rename that executable:

mv ./target/debug/show-version qqrq
./qqrq -V
show-version 1.2.3

So this always uses the name that was defined in Cargo.toml unlike the way we printed the usage message in ARGV - command line parameters for simple CLI program where we always print the name of the current executable.


Actually you can embed the version in the binary without Clap as well.

Related Pages

How to include sha1 from git in the version report using derive interface of Clap?
Embed version number in the binary compiled by Rust
Clap - short command line parameters
Clap - short command line parameters
Clap - Command Line Argument Parser for Rust


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