Checking Stock
Part of the Logic & Flow section of Coddy's Rust journey — lesson 51 of 66.
Challenge
EasyYou will receive four inputs. The first input is an item name to check. The second input is a comma-separated list of item names in the inventory. The third input is a comma-separated list of prices corresponding to each item. The fourth input is a comma-separated list of quantities corresponding to each item. Build a HashMap to store the inventory data where the key is the item name and the value is a tuple containing the price and quantity (f64, i32). Then check if the requested item exists in the inventory using .get() and print the appropriate stock information.
Requirements:
- Import
HashMapfromstd::collections - Read the first input (item name to check) and trim whitespace
- Read the second input (comma-separated item names) and trim whitespace
- Read the third input (comma-separated prices) and trim whitespace
- Read the fourth input (comma-separated quantities) and trim whitespace
- Create a mutable
HashMap<String, (f64, i32)>for the inventory - Split the item names by commas
- Split the prices by commas and parse each to
f64 - Split the quantities by commas and parse each to
i32 - Insert each item into the inventory with its corresponding price and quantity as a tuple
- Use
.get()to check if the requested item exists in the inventory - Use a
matchexpression to handle theOptionreturned by.get() - In the
Somearm, extract the price and quantity from the tuple and print:[item_name] is in stockPrice: $[price]Quantity: [quantity]
- In the
Nonearm, print:[item_name] is not in stock
Input:
- First line: Item name to check (e.g.,
Laptop) - Second line: Comma-separated item names (e.g.,
Laptop,Mouse,Keyboard) - Third line: Comma-separated prices (e.g.,
999.99,25.50,75.00) - Fourth line: Comma-separated quantities (e.g.,
15,50,30)
Output:
- If the item exists in inventory:
- First line:
[item_name] is in stock - Second line:
Price: $[price] - Third line:
Quantity: [quantity]
- First line:
- If the item does not exist:
[item_name] is not in stock
Try it yourself
use std::collections::HashMap;
use std::io;
fn main() {
// Read the item name to check
let mut item_to_check = String::new();
io::stdin().read_line(&mut item_to_check).expect("Failed to read line");
let item_to_check = item_to_check.trim();
// Read the comma-separated item names
let mut items_input = String::new();
io::stdin().read_line(&mut items_input).expect("Failed to read line");
let items_input = items_input.trim();
// Read the comma-separated prices
let mut prices_input = String::new();
io::stdin().read_line(&mut prices_input).expect("Failed to read line");
let prices_input = prices_input.trim();
// Read the comma-separated quantities
let mut quantities_input = String::new();
io::stdin().read_line(&mut quantities_input).expect("Failed to read line");
let quantities_input = quantities_input.trim();
// TODO: Write your code below
// Create a HashMap to store inventory data
// Split the inputs and populate the HashMap
// Use .get() to check if the item exists
// Use match to handle the Option and print the appropriate output
}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