Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Building a CLI Application | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 31 of 50

·rust

Building a CLI Application

25 minutes

Course progress62%

Building a CLI Application

Use the clap crate to parse arguments and subcommands.

# Cargo.toml
[dependencies]
clap = { version = "4.0", features = ["derive"] }
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = \"calc\")] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { Add { a: i32, b: i32 }, Sub { a: i32, b: i32 }, } fn main() { let cli = Cli::parse(); match cli.command { Commands::Add { a, b } => println!(\"{} + {} = {}\", a, b, a + b), Commands::Sub { a, b } => println!(\"{} - {} = {}\", a, b, a - b), } }
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonCommand Line ArgumentsContinue to lesson 32Using External CratesBack to Rust Fundamentals curriculum