Hone logo
Hone
Problems

Mutable References in Rust: Managing Shared State

Rust's ownership system is a cornerstone of its memory safety guarantees. However, sometimes you need to allow multiple parts of your code to modify the same data. This challenge focuses on creating and using mutable references (&mut) to achieve this safely, demonstrating how to manage shared, mutable state in Rust while adhering to the borrow checker's rules.

Problem Description

You are tasked with creating a function that modifies a vector of integers passed to it as a mutable reference. The function should double each element in the vector. The key requirement is to correctly use mutable references to avoid ownership conflicts and ensure that the original vector is modified in place. You must demonstrate understanding of how mutable references work and how to satisfy the borrow checker.

What needs to be achieved:

  • Write a function double_vector that takes a mutable reference to a vector of integers (&mut Vec<i32>).
  • Inside the function, iterate through the vector and double the value of each element.
  • The original vector passed to the function should be modified directly.

Key Requirements:

  • The function must accept a mutable reference (&mut).
  • The function must modify the vector in place.
  • The code must compile without borrow checker errors.

Expected Behavior:

When the double_vector function is called with a vector, the function should modify the vector's elements, doubling each integer. The caller should observe the changes to the original vector.

Edge Cases to Consider:

  • An empty vector: The function should handle an empty vector gracefully without panicking.
  • Vectors containing negative numbers: The doubling operation should work correctly for negative numbers.

Examples

Example 1:

Input: vec![1, 2, 3]
Output: vec![2, 4, 6]
Explanation: The `double_vector` function iterates through the input vector [1, 2, 3] and multiplies each element by 2, resulting in the modified vector [2, 4, 6].

Example 2:

Input: vec![-1, 0, 1]
Output: vec![-2, 0, 2]
Explanation: The function correctly doubles the negative number -1 to -2 and the positive number 1 to 2, leaving 0 unchanged.

Example 3:

Input: vec![]
Output: vec![]
Explanation: The function handles an empty vector gracefully, performing no operations and returning the empty vector unchanged.

Constraints

  • The input vector will contain only integers (i32).
  • The vector can be of any size (including empty).
  • The function must be efficient; avoid unnecessary allocations.
  • The solution must compile and run without panicking.

Notes

  • Remember that a mutable reference grants exclusive access to the data it points to.
  • You cannot have multiple mutable references to the same data at the same time.
  • Consider using a for loop or an iterator to traverse the vector.
  • The goal is to demonstrate your understanding of mutable references and how to use them safely within Rust's ownership system.
Loading editor...
rust