0
0
Rustprogramming~30 mins

Formatting output in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Formatting output
๐Ÿ“– Scenario: You are creating a simple Rust program to display product prices clearly for a small shop.
๐ŸŽฏ Goal: Learn how to format output in Rust using println! with placeholders and formatting options.
๐Ÿ“‹ What You'll Learn
Create a dictionary (HashMap) with product names and prices
Create a variable for a price threshold
Use a loop to print products with prices formatted to 2 decimal places
Print the formatted output lines
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Formatting prices clearly is important in shops, invoices, and reports to make numbers easy to read and professional.
๐Ÿ’ผ Career
Many programming jobs require formatting output for user interfaces, reports, or logs, making this skill very useful.
Progress0 / 4 steps
1
Create the product prices HashMap
Create a HashMap called products with these exact entries: "Apple" with price 0.99, "Banana" with price 0.59, and "Cherry" with price 2.99.
Rust
Need a hint?

Use HashMap::new() and insert to add each product and price.

2
Add a price threshold variable
Inside the main function, create a variable called threshold and set it to 1.00.
Rust
Need a hint?

Use let threshold = 1.00; inside main.

3
Loop and format output
Use a for loop with variables product and price to iterate over products.iter(). Inside the loop, use println! to print each product and price formatted to 2 decimal places using {:.2}.
Rust
Need a hint?

Use for (product, price) in products.iter() and println!("{}: ${:.2}", product, price);.

4
Print products above threshold
Modify the for loop to print only products with a price greater than threshold. Use println! to print the product name and price formatted to 2 decimal places.
Rust
Need a hint?

Use an if statement inside the loop to check if *price > threshold.