Menu
Coddy logo textTech

Removing a Task

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

challenge icon

Challenge

Easy

You will receive three inputs: first, a comma-separated list of existing tasks; second, a task number to remove (1-based indexing); and third, a new task to add after removal. Read all inputs, create a vector from the tasks, remove the specified task, add the new task, and display the updated task list.

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 mutable vector and add each task to it
  • Read the second input and convert it to an integer (the task number to remove, using 1-based indexing)
  • Convert the 1-based index to 0-based and use .remove() to remove the task at that index
  • Read the third input containing a new task to add
  • Use .push() to add the new task to the vector
  • Print the total number of tasks in the format: Total tasks: X
  • Print each task on a separate line in the format: Task: [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 remove (1-based indexing)
  • Third line: A new task to add (e.g., Study for exam)

Output:

  • First line: Total tasks: X where X is the total number of tasks after removal and addition
  • Following lines: Each task printed as Task: [task description]

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 remove (1-based indexing)
    let task_to_remove = lines.next().unwrap().unwrap();
    let task_number: usize = task_to_remove.trim().parse().unwrap();
    
    // Read the new task to add
    let new_task = lines.next().unwrap().unwrap();
    
    // TODO: Write your code below
    // Split the tasks_input by commas and create a mutable vector
    // Remove the task at the specified index (convert 1-based to 0-based)
    // Add the new task to the vector
    
    // Output the results
    // Print total tasks in format: Total tasks: X
    // Print each task in format: Task: [task description]
}

All lessons in Logic & Flow