Viewing All Tasks
Part of the Logic & Flow section of Coddy's Rust journey — lesson 17 of 66.
Challenge
EasyYou will receive two inputs: first, a comma-separated list of existing tasks, and second, a task number to view (1-based indexing). Read both inputs, create a vector from the tasks, and display all tasks with their numbers, highlighting the requested task.
Requirements:
- Read the first input containing comma-separated task descriptions (e.g.,
Buy groceries,Call dentist,Finish homework) - Split the string by commas to get individual tasks
- Create a vector and add each task to it
- Read the second input and convert it to an integer (the task number to highlight, using 1-based indexing)
- Print the total number of tasks in the format:
Total tasks: X - Iterate through the vector and print each task with its number (1-based)
- For the requested task number, print it in the format:
[X] [task description] (selected) - For all other tasks, print them in the format:
[X] [task description]
Input:
- First line: Comma-separated task descriptions (e.g.,
Buy groceries,Call dentist,Finish homework) - Second line: An integer representing the task number to highlight (1-based indexing)
Output:
- First line:
Total tasks: Xwhere X is the total number of tasks - Following lines: Each task with its number in the format
[X] [task description] - The selected task should have
(selected)appended to it
Try it yourself
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let mut lines = stdin.lock().lines();
// Read the comma-separated tasks
let tasks_input = lines.next().unwrap().unwrap();
// Read the task number to highlight
let task_number: usize = lines.next().unwrap().unwrap().trim().parse().unwrap();
// TODO: Write your code below
// Split the tasks_input by commas and create a vector
// Print the total number of tasks
// Iterate through the tasks and print each one with its number
// Highlight the selected task by appending " (selected)"
}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