Menu
Coddy logo textTech

Inserting Key-Value Pairs

Part of the Logic & Flow section of Coddy's Rust journey — lesson 29 of 66.

Now that you have an empty hash map, it's time to add some data to it. You can insert key-value pairs into a hash map using the .insert() method:

let mut capitals = HashMap::new();
capitals.insert("France", "Paris");
capitals.insert("Japan", "Tokyo");

The .insert() method takes two parameters: the key and the value. In this example, we're storing country names as keys and their capital cities as values.

There's an important behavior to remember: if you insert a key that already exists in the hash map, the new value will overwrite the old one:

capitals.insert("France", "Lyon");  // This overwrites "Paris"

After this operation, looking up "France" would return "Lyon" instead of "Paris". This overwriting behavior makes .insert() useful for both adding new entries and updating existing ones in your hash map.

challenge icon

Challenge

Easy

You will receive three inputs: a product name, its price (as a decimal number), and another product name. Create a mutable hash map to store product names as keys (type String) and their prices as values (type f64). Insert the first product with its price into the hash map. Then insert the second product with a price of 0.0. Finally, update the second product's price to 15.99 by inserting it again with the new price. Print all the information in the exact format shown below.

Note on .clone(): When inserting a String variable as a key into a HashMap, Rust's ownership rules mean the variable would be moved into the map and no longer usable afterward. To keep using the variable (e.g., for printing or inserting again), call .clone() on it when passing it to insert(). For example: map.insert(name.clone(), price).

Requirements:

  • Import HashMap from std::collections
  • Create a mutable hash map with types HashMap<String, f64>
  • Read the first input as the first product name
  • Read the second input and convert it to f64 for the first product's price
  • Insert the first product and its price into the hash map using .clone() on the product name
  • Read the third input as the second product name
  • Insert the second product with a price of 0.0 using .clone() on the product name
  • Insert the second product again with a price of 15.99 using .clone() (this will overwrite the previous value)
  • Print the information for both products in the exact format shown below

Input:

  • First line: First product name (e.g., Laptop)
  • Second line: First product price as a decimal number (e.g., 999.99)
  • Third line: Second product name (e.g., Mouse)

Output:

  • First line: Inserted [first_product] at $[price]
  • Second line: Inserted [second_product] at $0.00
  • Third line: Updated [second_product] to $15.99

Cheat sheet

To insert key-value pairs into a hash map, use the .insert() method:

let mut capitals = HashMap::new();
capitals.insert("France", "Paris");
capitals.insert("Japan", "Tokyo");

The .insert() method takes two parameters: the key and the value.

If you insert a key that already exists, the new value will overwrite the old one:

capitals.insert("France", "Lyon");  // This overwrites "Paris"

This overwriting behavior makes .insert() useful for both adding new entries and updating existing ones.

Try it yourself

use std::collections::HashMap;
use std::io;

fn main() {
    // Read inputs
    let mut product1 = String::new();
    io::stdin().read_line(&mut product1).expect("Failed to read line");
    let product1 = product1.trim().to_string();
    
    let mut price1_input = String::new();
    io::stdin().read_line(&mut price1_input).expect("Failed to read line");
    let price1: f64 = price1_input.trim().parse().expect("Failed to parse price");
    
    let mut product2 = String::new();
    io::stdin().read_line(&mut product2).expect("Failed to read line");
    let product2 = product2.trim().to_string();
    
    // TODO: Write your code below
    // Create a mutable HashMap, insert products, and print the required output
    
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow