📖 Scenario: Imagine you run a small bakery. Every day, you bake different types of bread and want to keep track of how many of each type you have ready to sell. You need a way to store these numbers so you can quickly check and update them.
🎯 Goal: Build a simple program using an array to store the number of breads of different types. This will show why arrays are useful to keep related data together and access it easily by position.
📋 What You'll Learn
Create an array called breads with 5 elements representing counts of bread types
Add a variable called total_breads to hold the sum of all breads
Use a for loop with variable count to add all bread counts into total_breads
Print the total_breads value
💡 Why This Matters
🌍 Real World
Arrays are used in many real-life programs to keep track of lists of items, like inventory counts, scores, or measurements.
💼 Career
Understanding arrays is a basic skill for programming jobs, as they are the foundation for storing and managing collections of data efficiently.
Progress0 / 4 steps
1
Create an array to store bread counts
Create an array called breads with these exact counts: 10, 15, 7, 20, 5
DSA Rust
Hint
Use square brackets to create an array with the given numbers.
2
Add a variable to hold the total count
Add a variable called total_breads and set it to 0 before the loop
DSA Rust
Hint
Use let mut to create a variable that can change.
3
Sum all bread counts using a for loop
Use a for loop with variable count to add each element of breads to total_breads
DSA Rust
Hint
Use for count in breads to loop through the array.
4
Print the total number of breads
Print the value of total_breads using println!
DSA Rust
Hint
Use println!("{}", total_breads); to print the number.
Practice
(1/5)
1. Why do arrays exist in programming languages like Rust?
easy
A. To store multiple values of the same type in a fixed-size, ordered collection
B. To store values of different types without any order
C. To perform mathematical calculations faster than variables
D. To replace functions with data
Solution
Step 1: Understand array purpose and properties
Arrays are designed to hold multiple values of the same type in a fixed-size, ordered collection, unlike other structures.
Final Answer:
To store multiple values of the same type in a fixed-size, ordered collection -> Option A
Quick Check:
Arrays = fixed-size, same type, ordered [OK]
Hint: Arrays hold same-type items in order with fixed size [OK]
Common Mistakes:
Thinking arrays can hold different types
Confusing arrays with lists or sets
Assuming arrays are dynamic in size
2. Which of the following is the correct way to declare an array of 5 integers in Rust?
easy
A. let arr = <1, 2, 3, 4, 5>;
B. let arr = (1, 2, 3, 4, 5);
C. let arr = [1, 2, 3, 4, 5];
D. let arr = {1, 2, 3, 4, 5};
Solution
Step 1: Recall Rust array syntax and check options
Rust arrays use square brackets [] with values. let arr = [1, 2, 3, 4, 5]; correct; ()=tuple, {}=invalid, <=invalid.
Final Answer:
let arr = [1, 2, 3, 4, 5]; -> Option C
Quick Check:
Rust arrays use [] brackets [OK]
Hint: Use square brackets [] for arrays in Rust [OK]
Common Mistakes:
Using parentheses () which create tuples
Using braces {} which are for blocks or sets in other languages
Using angle brackets <> which are invalid here
3. What will be the output of this Rust code?
let arr = [10, 20, 30, 40, 50];
println!("{}", arr[2]);