Hone logo
Hone
Problems

Implementing a Simple Command-Line Task Manager in Rust

This challenge asks you to build a basic command-line task manager using Rust. The task manager should allow users to add tasks, list tasks, and mark tasks as complete. This exercise will help you practice fundamental Rust concepts like structs, enums, vectors, and basic input/output operations.

Problem Description

You are tasked with creating a command-line application that manages a list of tasks. Each task has a description and a status (incomplete or complete). The application should support the following commands:

  • add <task_description>: Adds a new task with the given description and an incomplete status.
  • list: Lists all tasks, displaying their index, description, and status. Incomplete tasks should be marked with [ ], and complete tasks with [x].
  • complete <task_index>: Marks the task at the given index as complete.
  • exit: Exits the application.

The application should continuously prompt the user for commands until the exit command is entered. Error handling is crucial; the application should gracefully handle invalid commands and invalid task indices.

Key Requirements:

  • Use a struct to represent a task, including fields for the description (String) and status (enum).
  • Use an enum to represent the task status (Incomplete, Complete).
  • Store tasks in a Vec.
  • Implement the four commands as described above.
  • Provide clear and informative error messages to the user.
  • The task index starts from 1 (not 0).

Expected Behavior:

The application should:

  1. Start by displaying a welcome message.
  2. Continuously prompt the user for a command.
  3. Parse the command and its arguments.
  4. Perform the requested action.
  5. Display appropriate output or error messages.
  6. Exit gracefully when the exit command is entered.

Edge Cases to Consider:

  • Empty task list when list command is entered.
  • Invalid task index provided to the complete command (e.g., index out of bounds, non-numeric input).
  • Invalid command entered by the user.
  • Empty task description provided to the add command (should still add a task with an empty description).

Examples

Example 1:

Input:
add Buy groceries
add Walk the dog
list
complete 1
list
exit
Output:
Welcome to the Task Manager!
1. [ ] Buy groceries
2. [ ] Walk the dog
1. [x] Buy groceries
2. [ ] Walk the dog

Example 2:

Input:
list
complete 5
exit
Output:
Welcome to the Task Manager!
No tasks yet.
Invalid task index: 5

Example 3: (Edge Case - Invalid Command)

Input:
invalid_command
exit
Output:
Welcome to the Task Manager!
Invalid command: invalid_command

Constraints

  • The maximum length of a task description is 256 characters.
  • The number of tasks that can be added is limited to 100. This is to prevent excessive memory usage.
  • Input should be read from standard input (stdin) and output to standard output (stdout).
  • The application should be reasonably responsive; avoid unnecessary delays.

Notes

  • Consider using match statements for command parsing.
  • Error handling is a key aspect of this challenge. Provide helpful messages to guide the user.
  • You can use the std::io module for input/output operations.
  • Think about how to handle potential errors when parsing user input (e.g., converting a string to an integer).
  • Focus on clarity and readability of your code. Use meaningful variable names and comments where appropriate.
  • The exit command should terminate the program immediately.
Loading editor...
rust