Implementing a Custom Iterator in Rust
Rust's iterator pattern is a powerful and idiomatic way to process sequences of data. By implementing the Iterator trait, you can create custom data structures that can be seamlessly used with Rust's rich collection processing methods like map, filter, fold, and collect. This challenge will guide you through creating your own iterator for a custom data structure.
Problem Description
Your task is to implement the Iterator trait for a custom struct that represents a sequence of numbers. Specifically, you will create an iterator that generates a sequence of numbers starting from a given value, incrementing by a specified step, and stopping after a certain count.
You need to:
- Define a custom struct that will hold the state for your iterator (e.g., current value, step, remaining count).
- Implement the
Iteratortrait for this struct. The core of this is thenext()method, which should return anOption<Self::Item>. - The
next()method should returnSome(value)for each number in the sequence andNonewhen the sequence is exhausted.
Key Requirements:
- The iterator should start generating numbers from an initial
start_value. - Each subsequent number should be generated by adding a
stepto the previous one. - The iterator should produce a total of
countnumbers. - The
Itemtype of the iterator should be an integer type (e.g.,i32).
Expected Behavior:
- When
next()is called repeatedly, it should return the numbers in the sequence one by one. - After
countcalls tonext(), subsequent calls should returnNone.
Edge Cases to Consider:
countbeing 0.- Negative
start_valueorstep. - A
stepof 0.
Examples
Example 1:
Input: start_value = 5, step = 2, count = 3
Iterator output:
5
7
9
Explanation: The iterator starts at 5. The next number is 5 + 2 = 7. The next number is 7 + 2 = 9. The count is 3, so after 9, the iterator stops.
Example 2:
Input: start_value = 10, step = -3, count = 4
Iterator output:
10
7
4
1
Explanation: The iterator starts at 10. The next number is 10 + (-3) = 7. The next is 7 + (-3) = 4. The next is 4 + (-3) = 1. The count is 4, so after 1, it stops.
Example 3:
Input: start_value = 0, step = 5, count = 0
Iterator output:
(no output)
Explanation: When the count is 0, the iterator should immediately return None on the first call to next(), producing no elements.
Constraints
start_value: Ani32integer.step: Ani32integer.count: Ausizeinteger, representing the number of elements to generate.countwill be non-negative.- The maximum value of
countwill be within typicalusizelimits. - The intermediate and final values of the numbers generated should not overflow
i32.
Notes
- You'll need to define a struct to hold the
current_value,step, andremaining_count. - The
Iteratortrait requires you to define an associated typeItem. For this problem,Itemshould bei32. - Think about how to manage the state within your struct so that
next()can correctly return the next value and eventuallyNone. - You can use the
impl Iterator for YourStructName {}syntax to implement the trait.