Building a Command-Line App
Combine functions, input, collections, and error handling into a small CLI project.
Learning objectives
- Read simple user input
- Store data in a vector
- Structure a small command-line program
Command-Line Thinking
A command-line app accepts text input and produces text output. Rust is a strong language for building fast and reliable CLI tools.
Project Structure
A small CLI can begin in one file. As the app grows, you can move code into modules.
Mini Project
The example below stores a few tasks in a vector and prints them as a simple task list.
Example code
fn main() {
let mut tasks: Vec<String> = Vec::new();
tasks.push(String::from("Learn ownership"));
tasks.push(String::from("Practice structs"));
tasks.push(String::from("Build a CLI app"));
println!("Task List");
for (index, task) in tasks.iter().enumerate() {
println!("{}. {}", index + 1, task);
}
}
Practice task: Extend the task list program by adding a completed field using a struct.