Hone logo
Hone
Problems

Iterating with while let: Processing Optional Data

The while let construct in Rust is a powerful tool for iterating over data that might be present or absent, typically when dealing with Option or Result types. This challenge focuses on understanding and applying while let to process a sequence of optional values, mimicking scenarios where you might be fetching data or processing items that could be incomplete.

Problem Description

Your task is to implement a function that consumes a Vec<Option<String>> and processes each Some variant until a None variant is encountered or the vector is exhausted. The function should collect all the String values from the Some variants into a new Vec<String>.

Key Requirements:

  • The function must use a while let loop to iterate through the input vector.
  • It should only process Some(String) variants.
  • The loop should terminate as soon as a None variant is encountered.
  • The function should return a Vec<String> containing all the extracted string values in the order they appeared.

Expected Behavior:

Given an input vector of Option<String>, the function should iterate, unwrapping Some values and ignoring None values until a None is found. All successfully unwrapped strings should be accumulated.

Edge Cases to Consider:

  • An empty input vector.
  • A vector that starts with None.
  • A vector that contains only Some values (no None termination).
  • A vector with multiple consecutive None values.

Examples

Example 1:

Input: vec![Some("hello".to_string()), Some("world".to_string()), None, Some("rust".to_string())]
Output: vec!["hello".to_string(), "world".to_string()]
Explanation: The loop processes "hello" and "world". When it encounters `None`, the loop terminates. The subsequent `Some("rust")` is not processed.

Example 2:

Input: vec![Some("first".to_string()), Some("second".to_string()), Some("third".to_string())]
Output: vec!["first".to_string(), "second".to_string(), "third".to_string()]
Explanation: The input vector does not contain a `None`. The loop processes all elements until the vector is exhausted, collecting all `Some` values.

Example 3:

Input: vec![None, Some("should not be here".to_string()), Some("neither".to_string())]
Output: vec![]
Explanation: The loop immediately encounters `None` and terminates without processing any `Some` values.

Example 4:

Input: vec![]
Output: vec![]
Explanation: An empty input vector results in an empty output vector as there are no elements to process.

Constraints

  • The input will always be a Vec<Option<String>>.
  • The function should be reasonably efficient; no excessive copying or complex algorithms are expected.

Notes

  • Consider how to access elements of the Vec one by one without consuming the entire Vec upfront, or how to iterate over a mutable reference to the Vec.
  • The while let syntax is crucial here. Think about the pattern you'll match against.
  • This problem is designed to reinforce idiomatic Rust patterns for handling optional data.
Loading editor...
rust