Adding an Item
Part of the Logic & Flow section of Coddy's Rust journey — lesson 50 of 66.
Challenge
EasyYou will receive three inputs. The first input is an item name, the second input is the item's price, and the third input is the initial quantity in stock. Create a HashMap to store inventory data where the key is the item name and the value is a tuple containing the price and quantity (f64, i32). Add the item to the inventory and print a confirmation message followed by the current inventory details.
Requirements:
- Import
HashMapfromstd::collections - Read the first input (item name) and trim whitespace
- Read the second input (price) and trim whitespace
- Parse the price to
f64 - Read the third input (quantity) and trim whitespace
- Parse the quantity to
i32 - Create a mutable
HashMap<String, (f64, i32)>for the inventory - Insert the item into the inventory with the price and quantity as a tuple
- Print:
Added [item_name] to inventory - Print:
Price: $[price] - Print:
Quantity: [quantity]
Input:
- First line: Item name (e.g.,
Laptop) - Second line: Price as a number (e.g.,
999.99) - Third line: Quantity as a number (e.g.,
15)
Output:
- First line:
Added [item_name] to inventory - Second line:
Price: $[price] - Third line:
Quantity: [quantity]
Try it yourself
use std::collections::HashMap;
use std::io;
fn main() {
// Read item name
let mut item_name = String::new();
io::stdin().read_line(&mut item_name).expect("Failed to read line");
let item_name = item_name.trim().to_string();
// Read price
let mut price_input = String::new();
io::stdin().read_line(&mut price_input).expect("Failed to read line");
let price: f64 = price_input.trim().parse().expect("Failed to parse price");
// Read quantity
let mut quantity_input = String::new();
io::stdin().read_line(&mut quantity_input).expect("Failed to read line");
let quantity: i32 = quantity_input.trim().parse().expect("Failed to parse quantity");
// TODO: Write your code below
// Create a HashMap to store inventory
// Add the item to the inventory
// Print the confirmation messages
}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