šŸ’»
Hands-on Project

CLI Todo Application

Build a command-line todo app with file persistence

Rust CLI Tool Project

A simple command-line interface tool for managing a to-do list, demonstrating practical Rust concepts.

Concepts Covered

This project demonstrates several important Rust concepts:

  1. Command-line argument parsing using clap
  2. Serialization and deserialization with serde and serde_json
  3. File I/O operations with std::fs
  4. Error handling with Result types
  5. Ownership and borrowing when manipulating data structures
  6. Pattern matching with enums and match expressions

Project Structure

cli-tool/
ā”œā”€ā”€ Cargo.toml
ā”œā”€ā”€ src/
│   └── main.rs
└── todo.json (created when you add tasks)

Setup

  1. Make sure you have Rust and Cargo installed:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. Navigate to the project directory:

    cd rust-tutorial/projects/cli-tool
  3. Build the project:

    cargo build

Usage

Run the CLI tool with various commands:

Add a task

cargo run -- add "Learn Rust"
cargo run -- add "Build a CLI tool"

List all tasks

cargo run -- list

Complete a task

cargo run -- complete 1

Remove a task

cargo run -- remove 2

How It Works

The CLI tool stores tasks in a JSON file (todo.json) in the current directory. It uses serde to serialize and deserialize the task list, making it persistent between runs.

Key Components

  1. Cli struct - Defines the command-line interface using clap attributes
  2. Commands enum - Represents the available subcommands (add, list, remove, complete)
  3. Task struct - Represents a single task with ID, description, and completion status
  4. TodoList struct - Manages a collection of tasks with methods for manipulation

Error Handling

The tool uses Rust's Result type for error handling, ensuring that file operations and task manipulations are safely handled.

Running Tests

To run the tests for this project:

cargo test

Building for Release

To build an optimized version for release:

cargo build --release

The executable will be in target/release/rust-tutorial-cli.

Extending the Project

Possible extensions for this project:

  1. Add due dates to tasks
  2. Implement task categories or tags
  3. Add a search functionality
  4. Implement task priorities
  5. Add recurring tasks

šŸ¦€ Rust Programming Tutorial

Learn from Zero to Advanced

Built with Next.js and Tailwind CSS • Open Source