Building a command-line to-do list in Rust

This post builds a small interactive to-do list that runs in the terminal. It uses only the standard library and covers the basics of a Rust CLI loop: reading lines from stdin, matching on commands, and passing a Vec around by shared and mutable reference.
The program supports four commands:
listshows the tasks with their numbersaddasks for a task and appends itremoveasks for a task number and deletes itquitexits
cargo new todo_listcd todo_listNo dependencies are needed. Everything below goes in src/main.rs.
Reading a line
Section titled “Reading a line”Every command needs to print a prompt and read a line, so start with a helper:
use std::io::{self, Write};
/// Print `message`, then read one line from stdin./// Returns `None` at end of input (Ctrl+D, or Ctrl+Z then Enter on Windows).fn prompt(message: &str) -> Option<String> { print!("{message}"); io::stdout().flush().ok()?;
let mut input = String::new(); match io::stdin().read_line(&mut input) { Ok(0) | Err(_) => None, Ok(_) => Some(input.trim().to_string()), }}Two details matter here:
print!doesn’t end with a newline, and stdout is line-buffered, so withoutflush()the prompt wouldn’t appear until after the user typed something.flushcomes from theWritetrait, which is why it has to be imported.read_linereturnsOk(0)at end of input. If you ignore that, the main loop spins forever on empty input when stdin is closed or piped from a file.
The command loop
Section titled “The command loop”fn run() { let mut todo_list: Vec<String> = Vec::new();
while let Some(command) = prompt("> ") { match command.as_str() { "list" => list_tasks(&todo_list), "add" => add_task(&mut todo_list), "remove" => remove_task(&mut todo_list), "quit" => break, "" => {} other => println!("Unknown command: {other}. Try list, add, remove or quit."), } }}
fn main() { run();}match on a String needs as_str(), because the patterns are string literals (&str). The loop ends either on quit or when prompt returns None.
Listing tasks
Section titled “Listing tasks”fn list_tasks(todo_list: &[String]) { if todo_list.is_empty() { println!("No tasks."); return; } for (i, task) in todo_list.iter().enumerate() { println!("{}. {task}", i + 1); }}The parameter is &[String] rather than &Vec<String>: a slice accepts a &Vec<String> (through deref coercion) and also arrays and sub-slices. The tasks are numbered from 1, since that’s what the user will type when removing one.
Adding a task
Section titled “Adding a task”fn add_task(todo_list: &mut Vec<String>) { match prompt("Task: ") { Some(task) if !task.is_empty() => { todo_list.push(task); println!("Added."); } _ => println!("Nothing added."), }}Removing a task
Section titled “Removing a task”The user might type something that isn’t a number, or a number that’s out of range. The original version of this post called parse().unwrap(), which crashes the program on abc. Handle both cases instead:
fn remove_task(todo_list: &mut Vec<String>) { let Some(input) = prompt("Task number: ") else { return };
match input.parse::<usize>() { Ok(n) if (1..=todo_list.len()).contains(&n) => { let task = todo_list.remove(n - 1); println!("Removed: {task}"); } Ok(_) => println!("No task with that number."), Err(_) => println!("Please enter a number."), }}Vec::remove shifts every later element down by one, so the remaining tasks are renumbered on the next list.
Running it
Section titled “Running it”cargo run> addTask: write blog postAdded.> addTask: review PRAdded.> list1. write blog post2. review PR> removeTask number: 1Removed: write blog post> list1. review PR> quitNext steps
Section titled “Next steps”The list lives in memory and disappears when the program exits. Natural next steps:
- Persist it. Write one task per line to a file with
std::fs::writeon each change and read it back at startup withstd::fs::read_to_string, or useserde_jsonfor structured tasks. - Mark tasks done instead of deleting them, by storing a small struct (
struct Task { text: String, done: bool }) rather than aString. - Use subcommands instead of a prompt (
todo add "write blog post"), withclapparsing the arguments. That makes the tool scriptable.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)