📖 Scenario: You are working on a small Rust program that needs to compare two collections of numbers. One collection is a fixed-size array, and the other is a dynamic vector. You want to check if they contain the same numbers in the same order.
🎯 Goal: Build a Rust program that creates an array and a vector with the same numbers, compares them, and prints whether they are equal or not.
📋 What You'll Learn
Create a fixed-size array called numbers_array with the values [10, 20, 30, 40, 50].
Create a vector called numbers_vector with the values vec![10, 20, 30, 40, 50].
Compare numbers_array and numbers_vector for equality.
Print "Equal" if they are the same, otherwise print "Not equal".
💡 Why This Matters
🌍 Real World
Comparing fixed-size and dynamic collections is common when processing data that may come from different sources or formats.
💼 Career
Understanding how to work with arrays and vectors and compare them is important for Rust developers working on performance-sensitive or data-processing applications.
Progress0 / 4 steps
1
Create the fixed-size array
Create a fixed-size array called numbers_array with these exact values: [10, 20, 30, 40, 50].
Rust
Hint
Use square brackets [] to create an array with fixed values.
2
Create the vector
Create a vector called numbers_vector with these exact values: vec![10, 20, 30, 40, 50].
Rust
Hint
Use vec![...] macro to create a vector with values.
3
Compare the array and vector
Write a let statement to create a boolean variable called are_equal that compares numbers_array and numbers_vector for equality using ==.
Rust
Hint
Convert the vector to a slice using .as_slice() before comparing to the array.
4
Print the comparison result
Write a println! statement that prints "Equal" if are_equal is true, otherwise prints "Not equal".
Rust
Hint
Use an if statement to check are_equal and print the correct message.
Practice
(1/5)
1. Which of the following statements correctly describes the main difference between a Rust array and a Rust vector?
easy
A. Arrays have a fixed size known at compile time, while vectors can grow or shrink at runtime.
B. Vectors have a fixed size known at compile time, while arrays can grow or shrink at runtime.
C. Both arrays and vectors have fixed sizes and cannot change after creation.
D. Arrays are only used for strings, while vectors are used for numbers.
Solution
Step 1: Understand array size behavior
Arrays in Rust have a fixed size that must be known when the program is compiled.
Step 2: Understand vector size behavior
Vectors are dynamic and can grow or shrink during program execution.
Final Answer:
Arrays have a fixed size known at compile time, while vectors can grow or shrink at runtime. -> Option A