Recap - Simple Command Parser
Part of the Logic & Flow section of Coddy's Rust journey — lesson 6 of 66.
Challenge
EasyYou will receive a command string as input. Read the input and use the control flow techniques you've learned to parse and respond to different commands.
Requirements:
- Read a command string from input
- Use a
matchexpression to handle the following commands:"start"→ print"System starting...""stop"→ print"System stopping...""pause"or"wait"→ print"System paused""status"→ print"System running"- Any other command → print
"Unknown command"
Input: A single command string
Output: Print the appropriate response based on the command
Try it yourself
use std::io;
fn main() {
// Read the command from input
let mut command = String::new();
io::stdin().read_line(&mut command).expect("Failed to read line");
let command = command.trim();
// TODO: Write your code below
// Use a match expression to handle different commands
}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