Menu
Coddy logo textTech

Viewing All Tasks

Part of the Logic & Flow section of Coddy's Rust journey — lesson 17 of 66.

challenge icon

Challenge

Easy

You 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: X where 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