Building CLI Applications in Rust
In this lesson, you will learn how to build Command Line Interface (CLI) applications using Rust.
CLI tools are widely used for automation, DevOps, system utilities, and developer tooling. Rust is an excellent choice for CLI applications because it produces fast, portable, single-binary executables.
What Is a CLI Application?
A CLI application is a program that runs in the terminal and accepts input through command-line arguments instead of a graphical interface.
Examples of CLI tools include:
gitcargodockerkubectl
Why Use Rust for CLI Tools?
Rust is particularly well suited for building CLI applications because it offers:
- Fast startup time
- No runtime dependencies
- Strong error handling
- Cross-platform binaries
- Excellent ecosystem for CLI parsing
Creating a New CLI Project
Create a new Rust project using Cargo:
cargo new rust_cli_app
cd rust_cli_app
Reading Command-Line Arguments (Basic)
Rust provides access to command-line arguments using
the std::env::args function.
use std::env;
fn main() {
let args: Vec = env::args().collect();
println!("{:?}", args);
}
The first argument is always the program name.
Using the Clap Crate
For real-world CLI tools, manual argument parsing is not practical. The clap crate is the standard solution.
Add clap to your Cargo.toml:
[dependencies]
clap = { version = "4", features = ["derive"] }
Defining CLI Arguments
Use clap’s derive macros to define arguments cleanly.
use clap::Parser;
#[derive(Parser)]
struct Args {
#[arg(short, long)]
name: String,
}
fn main() {
let args = Args::parse();
println!("Hello, {}!", args.name);
}
Run the program like this:
cargo run -- --name Alice
Adding Subcommands
Many CLI tools support subcommands such as git commit.
Clap makes this easy.
use clap::{Parser, Subcommand};
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Add { value: i32 },
Subtract { value: i32 },
}
fn main() {
let cli = Cli::parse();
match cli.command {
Commands::Add { value } => println!("Result: {}", value + 10),
Commands::Subtract { value } => println!("Result: {}", value - 10),
}
}
Error Handling in CLI Apps
CLI tools must provide clear error messages.
Rust’s Result type makes this explicit.
fn parse_number(input: &str) -> Result {
input.parse::().map_err(|_| "Invalid number".to_string())
}
Clear errors improve usability and trust.
Packaging and Distribution
Rust CLI tools compile into a single binary. You can distribute them easily by sharing the executable.
Build a release binary using:
cargo build --release
Real-World Use Cases
- Automation scripts
- DevOps tooling
- System utilities
- Data processing tools
📝 Practice Exercises
Exercise 1
Create a CLI that accepts two numbers and prints their sum.
Exercise 2
Add a subcommand for multiplication.
Exercise 3
Display a custom help message.
✅ Practice Answers
Answer 1
Use clap arguments and parse them as integers.
Answer 2
Add a new enum variant under Subcommand.
Answer 3
Use clap’s built-in help and description attributes.
What’s Next?
In the next lesson, you will learn about WebAssembly (WASM) and how Rust can be compiled to run in the browser.