Bird
Raised Fist0
Rustprogramming~20 mins

Accessing elements in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Element Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code accessing a vector element?
Consider the following Rust code that accesses elements in a vector. What will it print?
Rust
fn main() {
    let numbers = vec![10, 20, 30, 40];
    println!("{}", numbers[2]);
}
AIndex out of bounds panic
B20
C40
D30
Attempts:
2 left
💡 Hint
Remember that Rust vectors are zero-indexed, so the first element is at index 0.
❓ Predict Output
intermediate
2:00remaining
What happens when accessing a vector element with an invalid index?
What will happen when running this Rust code?
Rust
fn main() {
    let letters = vec!['a', 'b', 'c'];
    println!("{}", letters[5]);
}
APanics with index out of bounds error
BPrints 'a'
CPrints 'c'
DPrints nothing
Attempts:
2 left
💡 Hint
Check what happens if you try to access an index outside the vector's length.
❓ Predict Output
advanced
2:00remaining
What is the output when using get() method to access vector elements?
Analyze this Rust code and determine its output.
Rust
fn main() {
    let nums = vec![1, 2, 3];
    match nums.get(1) {
        Some(val) => println!("{}", val),
        None => println!("No value"),
    }
    match nums.get(5) {
        Some(val) => println!("{}", val),
        None => println!("No value"),
    }
}
A1\n3
B2\nNo value
CNo value\nNo value
D2\n3
Attempts:
2 left
💡 Hint
The get() method returns an Option type. If the index is valid, it returns Some(value), else None.
❓ Predict Output
advanced
2:00remaining
What is the output of this code accessing elements in a tuple?
What will this Rust program print?
Rust
fn main() {
    let tup = (100, 200, 300);
    println!("{}", tup.1);
}
A200
B300
CCompilation error
D100
Attempts:
2 left
💡 Hint
Tuple elements are accessed by dot followed by the index starting at 0.
❓ Predict Output
expert
2:00remaining
What is the output of this Rust code accessing elements in a nested vector?
Examine the code and determine what it prints.
Rust
fn main() {
    let matrix = vec![
        vec![1, 2, 3],
        vec![4, 5, 6],
        vec![7, 8, 9],
    ];
    println!("{}", matrix[1][2]);
}
A9
B5
C6
DIndex out of bounds panic
Attempts:
2 left
💡 Hint
Remember that matrix[1] accesses the second vector, then [2] accesses its third element.

Practice

(1/5)
1. In Rust, how do you access the first element of an array named arr?
easy
A. arr[0]
B. arr[1]
C. arr.first()
D. arr.get(1)

Solution

  1. Step 1: Understand Rust indexing

    Rust arrays and slices use zero-based indexing, so the first element is at index 0.
  2. Step 2: Access the element using square brackets

    Using arr[0] accesses the first element directly.
  3. Final Answer:

    arr[0] -> Option A
  4. Quick Check:

    First element index = 0 [OK]
Hint: Remember: Rust arrays start at index 0 [OK]
Common Mistakes:
  • Using index 1 for the first element
  • Confusing .first() with indexing
  • Using get(1) which accesses second element
2. Which of the following is the correct syntax to access the third element of a vector v in Rust?
easy
A. v.get[3]
B. v(3)
C. v[3]
D. v[2]

Solution

  1. Step 1: Recall zero-based indexing

    The third element is at index 2 because counting starts at zero.
  2. Step 2: Use square brackets with index 2

    The correct syntax is v[2] to access the third element.
  3. Final Answer:

    v[2] -> Option D
  4. Quick Check:

    Third element index = 2 [OK]
Hint: Count from zero: third element is index 2 [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using index 3 for third element
  • Incorrect method call syntax like get[3]
3. What will be the output of this Rust code?
let arr = [10, 20, 30, 40];
println!("{}", arr[1]);
medium
A. 20
B. 40
C. 30
D. 10

Solution

  1. Step 1: Identify the array elements and their indexes

    The array arr has elements: 10 at index 0, 20 at index 1, 30 at index 2, and 40 at index 3.
  2. Step 2: Access element at index 1

    The code prints arr[1], which is 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    arr[1] = 20 [OK]
Hint: Index 1 means second element in Rust arrays [OK]
Common Mistakes:
  • Printing arr[0] instead of arr[1]
  • Confusing index with element value
  • Expecting output 30 or 40
4. Consider this Rust code snippet:
let v = vec![1, 2, 3];
let x = v[3];

What is the problem here?
medium
A. Syntax error: wrong vector declaration
B. Index out of bounds panic at runtime
C. Compile-time error: invalid index
D. No problem, x will be 3

Solution

  1. Step 1: Check vector size and index used

    The vector v has 3 elements at indexes 0, 1, and 2. Index 3 is out of range.
  2. Step 2: Understand Rust's runtime behavior

    Accessing v[3] causes a panic at runtime due to index out of bounds.
  3. Final Answer:

    Index out of bounds panic at runtime -> Option B
  4. Quick Check:

    Invalid index causes panic [OK]
Hint: Index must be less than vector length [OK]
Common Mistakes:
  • Thinking it causes compile error
  • Assuming x will be 3
  • Ignoring runtime panic possibility
5. You have a nested vector let data = vec![vec![1, 2], vec![3, 4], vec![5, 6]];. How do you access the number 4?
hard
A. data[0][1]
B. data[2][0]
C. data[1][1]
D. data[1][0]

Solution

  1. Step 1: Identify the outer and inner indexes

    The outer vector has 3 inner vectors: index 0 is [1, 2], index 1 is [3, 4], index 2 is [5, 6].
  2. Step 2: Access the inner element 4

    Within the inner vector at index 1 ([3, 4]), the element 4 is at index 1.
  3. Final Answer:

    data[1][1] -> Option C
  4. Quick Check:

    Nested access: outer 1, inner 1 = 4 [OK]
Hint: First index outer vec, second index inner vec [OK]
Common Mistakes:
  • Mixing up outer and inner indexes
  • Using wrong inner index for element 4
  • Accessing out-of-range indexes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard