Final Code
Part of the Logic & Flow section of Coddy's Rust journey — lesson 19 of 66.
Challenge
EasyYou will receive four inputs representing a complete to-do list workflow: first, a comma-separated list of initial tasks; second, a new task to add; third, a task number to view (1-based indexing); and fourth, a task number to remove (1-based indexing). Create a complete to-do list manager that processes all these operations and displays the final state.
Requirements:
- Read the first input containing comma-separated task descriptions (e.g.,
Buy groceries,Call dentist,Finish homework) - Split the string by commas and create a mutable vector with these initial tasks
- Read the second input and add this new task to the vector using
.push() - Read the third input, convert it to an integer (1-based task number to view)
- Read the fourth input, convert it to an integer (1-based task number to remove)
- Remove the specified task by converting the 1-based index to 0-based and using
.remove() - Print
Total tasks: Xwhere X is the final number of tasks - Print
Viewing task: Ywhere Y is the task number that was requested to view - Iterate through the final task list and print each task with its number (1-based)
- For the task matching the view number (after removal adjustments), print:
[X] [task description] (selected) - For all other tasks, print:
[X] [task description]
Input:
- First line: Comma-separated task descriptions (e.g.,
Buy groceries,Call dentist,Finish homework) - Second line: A new task to add (e.g.,
Study for exam) - Third line: An integer representing the task number to view (1-based indexing)
- Fourth line: An integer representing the task number to remove (1-based indexing)
Output:
- First line:
Total tasks: Xwhere X is the final number of tasks - Second line:
Viewing task: Ywhere Y is the requested view number - Following lines: Each task with its number in the format
[X] [task description] - The selected task (matching the view number) should have
(selected)appended
Try it yourself
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let mut lines = stdin.lock().lines();
// Read the initial comma-separated tasks
let initial_tasks = lines.next().unwrap().unwrap();
// Read the new task to add
let new_task = lines.next().unwrap().unwrap();
// Read the task number to view (1-based)
let view_number: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();
// Read the task number to remove (1-based)
let remove_number: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();
// TODO: Write your code below
// 1. Split initial_tasks by commas and create a mutable vector
// 2. Add the new_task to the vector
// 3. Remove the task at remove_number (convert to 0-based index)
// 4. Print the total tasks and viewing task information
// 5. Iterate through the final list and print each task
}All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together