How to correctly use iterator strucs and filters in generic traits

⚓ Rust    📅 2025-11-10    👤 surdeus    👁️ 4      

surdeus

I have this code which tries to create a tree of groups of data which are at the same "distance" from one another.

#[derive(Debug, PartialEq)]
pub struct Node
{
    value: u32,
    distance: u32,
    childs: Vec<Self>,
}
pub trait IterClone: Iterator + Clone {}

impl Node {

    pub fn new<I, C>(value: u32, distance: u32, mut iter: I, c: &C) -> Self where
    I: Iterator<Item = u32> + Clone,
    C: Fn(u32, u32) ->  u32
    {
        let mut r = Self {
            value,
            distance,
            childs: vec![],
        };

        while let Some(x) = iter.next() {
            r.childs.push(
                Self::new(x, r.distance, iter.clone().filter(|&v| c(v, value) == distance), c)
            )
        }
        r
    }
}

fn main() {
     let hamming = |a: u32, b: u32| -> u32
    {
        (a ^ b).count_ones()
    };

  let node = Node::new(0, 2, (0..(u8::MAX as u32)).filter(|&v| hamming(v, 0) == 2), &hamming);

  println!("{:?}", node);

}

I want the new function to take some kind of iterator as input as I don't want to have to allocate the input in memory.

The issue here seems to be that the new need to be implemented ( monomorphed?) for each actual type of I : Filter::<range, _>,
Filter::<Filter::<Range, _>, _>,
Filter::<Filter::<Filter::<Range, _>, _>, _>,
....

Compiling demo v0.1.0 (/home/jsbjr/dev/demo)
error[E0275]: overflow evaluating the requirement `for<'a> {closure@src/main.rs:37:59: 37:63}: FnMut(&'a _)`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`demo`)
  = note: required for `Filter<Range<u32>, ...>` to implement `Iterator`
  = note: 128 redundant requirements hidden
  = note: required for `Filter<..., ...>` to implement `Iterator`
  = note: the full name for the type has been written to '/home/jsbjr/dev/demo/target/debug/deps/demo-82a3ee1db27297eb.long-type-3730044516670567393.txt'
  = note: consider using `--verbose` to print the full type name to the console

For more information about this error, try `rustc --explain E0275`.
error: could not compile `demo` (bin "demo") due to 1 previous error

I tried to hide the type with a Box< dyn Iterator<Item = u32>> but then I can't clone the iterator to pass it to the next Nodes.

And I can't use Box< dyn Iterator<Item = u32> + Clone> because is sized and therefore not allowed for dynamic trait composition.

3 posts - 2 participants

Read full topic

🏷️ Rust_feed