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:
- Command-line argument parsing using
clap - Serialization and deserialization with
serdeandserde_json - File I/O operations with
std::fs - Error handling with
Resulttypes - Ownership and borrowing when manipulating data structures
- Pattern matching with enums and match expressions
Project Structure
cli-tool/
āāā Cargo.toml
āāā src/
ā āāā main.rs
āāā todo.json (created when you add tasks)Setup
Make sure you have Rust and Cargo installed:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shNavigate to the project directory:
cd rust-tutorial/projects/cli-toolBuild 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 -- listComplete a task
cargo run -- complete 1Remove a task
cargo run -- remove 2How 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
- Cli struct - Defines the command-line interface using
clapattributes - Commands enum - Represents the available subcommands (add, list, remove, complete)
- Task struct - Represents a single task with ID, description, and completion status
- 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 testBuilding for Release
To build an optimized version for release:
cargo build --releaseThe executable will be in target/release/rust-tutorial-cli.
Extending the Project
Possible extensions for this project:
- Add due dates to tasks
- Implement task categories or tags
- Add a search functionality
- Implement task priorities
- Add recurring tasks