Hone logo
Hone
Problems

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:

  1. Define a custom struct that will hold the state for your iterator (e.g., current value, step, remaining count).
  2. Implement the Iterator trait for this struct. The core of this is the next() method, which should return an Option<Self::Item>.
  3. The next() method should return Some(value) for each number in the sequence and None when 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 step to the previous one.
  • The iterator should produce a total of count numbers.
  • The Item type 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 count calls to next(), subsequent calls should return None.

Edge Cases to Consider:

  • count being 0.
  • Negative start_value or step.
  • A step of 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: An i32 integer.
  • step: An i32 integer.
  • count: A usize integer, representing the number of elements to generate. count will be non-negative.
  • The maximum value of count will be within typical usize limits.
  • 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, and remaining_count.
  • The Iterator trait requires you to define an associated type Item. For this problem, Item should be i32.
  • Think about how to manage the state within your struct so that next() can correctly return the next value and eventually None.
  • You can use the impl Iterator for YourStructName {} syntax to implement the trait.
Loading editor...
rust