Menu
Coddy logo textTech

Adding a Task

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

challenge icon

Challenge

Easy

You will receive two inputs: first, a comma-separated list of existing tasks, and second, a new task to add. Read both inputs, create a vector from the existing tasks, add the new task to the vector, and print the updated task list.

Requirements:

  • Read the first input containing comma-separated task descriptions (e.g., Buy groceries,Call dentist)
  • Split the string by commas to get individual tasks
  • Create a mutable vector and add each existing task to it
  • Read the second input containing the 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)
  • Second line: A new task to add (e.g., Finish homework)

Output:

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

Try it yourself

use std::io;

fn main() {
    // Read the first input (comma-separated tasks)
    let mut existing_tasks = String::new();
    io::stdin().read_line(&mut existing_tasks).expect("Failed to read line");
    let existing_tasks = existing_tasks.trim();
    
    // Read the second input (new task to add)
    let mut new_task = String::new();
    io::stdin().read_line(&mut new_task).expect("Failed to read line");
    let new_task = new_task.trim();
    
    // TODO: Write your code below
    // Split existing_tasks by commas and create a mutable vector
    // Add the new task to the vector
    // Print the total number of tasks
    // Print each task in the required format
    
}

All lessons in Logic & Flow