📖 Scenario: You are managing a list of daily temperatures recorded in a week. Sometimes, you need to check the temperature of a specific day or update it if there was a mistake in recording.
🎯 Goal: Build a Rust program that creates an array of temperatures, sets a specific day index to a new temperature, and then prints the updated array.
📋 What You'll Learn
Create an array called temperatures with these exact values: [23, 25, 22, 20, 24, 26, 21]
Create a variable called day_index and set it to 3
Update the temperature at day_index in temperatures to 19
Print the temperatures array after the update
💡 Why This Matters
🌍 Real World
Updating values in a list is common when managing daily records like temperatures, sales, or attendance.
💼 Career
Understanding how to access and update array elements is fundamental for programming tasks in software development, data processing, and system management.
Progress0 / 4 steps
1
Create the temperatures array
Create an array called temperatures with these exact values: [23, 25, 22, 20, 24, 26, 21]
DSA Rust
Hint
Use let mut temperatures = [23, 25, 22, 20, 24, 26, 21]; to create a mutable array.
2
Set the day index variable
Create a variable called day_index and set it to 3
DSA Rust
Hint
Use let day_index = 3; to create the index variable.
3
Update the temperature at the given index
Update the temperature at day_index in temperatures to 19
DSA Rust
Hint
Use temperatures[day_index] = 19; to update the array value.
4
Print the updated temperatures array
Print the temperatures array after the update using println!
DSA Rust
Hint
Use println!("{:?}", temperatures); to print the array.
Practice
(1/5)
1. What does the expression arr[2] do in Rust when arr is an array?
easy
A. Removes the element at index 2
B. Adds 2 to every element in the array
C. Accesses the element at index 2 of the array
D. Returns the length of the array
Solution
Step 1: Understand array indexing
In Rust, arrays use zero-based indexing, so arr[2] accesses the third element.
Step 2: Identify the operation
The expression retrieves the value stored at index 2 without modifying the array.
Final Answer:
Accesses the element at index 2 of the array -> Option C
Quick Check:
arr[2] means element at index 2 [OK]
Hint: Remember arrays start at index 0 in Rust [OK]
Common Mistakes:
Thinking indexing starts at 1
Confusing access with modification
Assuming it returns array length
2. Which of the following is the correct syntax to update the element at index 1 of a mutable array arr to 10 in Rust?
easy
A. arr[1] = 10;
B. arr(1) = 10;
C. arr{1} = 10;
D. arr->1 = 10;
Solution
Step 1: Recall Rust array update syntax
In Rust, to update an element, use square brackets with the index and assign a new value.
Step 2: Check each option
Only arr[1] = 10; uses correct square bracket indexing and assignment syntax.
Final Answer:
arr[1] = 10; -> Option A
Quick Check:
Use square brackets for indexing and assignment [OK]
Hint: Use square brackets [] for indexing and assignment [OK]
Common Mistakes:
Using parentheses () instead of brackets
Using curly braces {} for indexing
Using arrow operator -> incorrectly
3. What will be the output of the following Rust code?
The array arr is declared without mut, so it is immutable and cannot be changed.
Step 2: Identify assignment error
The line arr[1] = 5; tries to modify an immutable array element, causing a compile-time error.
Final Answer:
Cannot assign to element of immutable array -> Option A
Quick Check:
Immutable arrays cannot be updated [OK]
Hint: Declare array with 'mut' to allow updates [OK]
Common Mistakes:
Ignoring mut keyword for mutability
Assuming index out of bounds error
Thinking syntax error causes failure
5. Given a mutable array arr of length 4 initialized as [2, 4, 6, 8], which code snippet correctly doubles the value at index 3 and then prints the updated array?
hard
A. arr[4] = arr[4] * 2;
println!("{:?}", arr);
B. arr[3] = arr[3] * 2;
println!("{:?}", arr);
C. arr[3] *= 2
println!("{:?}", arr);
D. arr[3] = arr[3] + 2;
println!("{:?}", arr);
Solution
Step 1: Identify correct index and operation
Index 3 is the fourth element (value 8). Doubling means multiplying by 2, so arr[3] = arr[3] * 2; is correct.
Step 2: Check syntax and print statement
arr[3] = arr[3] * 2;println!("{:?}", arr); uses correct syntax with semicolon and prints the updated array properly.
Final Answer:
arr[3] = arr[3] * 2;
println!("{:?}", arr); -> Option B
Quick Check:
Multiply element at index 3 by 2, then print [OK]
Hint: Use correct index and multiply, remember semicolons [OK]
Common Mistakes:
Using index 4 which is out of bounds
Forgetting semicolon after assignment
Adding 2 instead of multiplying by 2
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.