Hi,
currently I’m writing a crate for conversion between Semantic Web’s RDF data and Rust types. The idea I’m stugglling with is the conversion of complex types into a semantic representation, i.e. a bunch of RDF-triples. Since the creation of those triples could be expensive I want to return an iterator that lazily creates the triples on demand. Therefore, I wrote the following code.

The plan is that implementors collect a bunch of closures that are executet on call of next(). It is intended that TriplesIter (and its closures) borrows self immutable so it can’t be altered while TriplesIter exists.

Would you consider this an idomatic solution? Some suggestions for impovements in terms of performance and/or ergonomics?

As you can see a clone() on the temporary dep is required. I thought this wouldn’t be necessary because the dep is moved into the closure?

each closure c / creator is intended to be called only once each, but you have used the Fn trait, which requires that it be possible to call the closure multiple times (and concurrently). Since the closure returns an owned HashSet, it needs to .clone() to be able to give multiple owned HashSets, else it wouldn’t be Fn (just FnOnce).

Given your use case, using Fn instead of FnOnce in the TripleCreatortype alias does not seem to be required, so you can loosen the trait requirement by using FnOnce instead, at which point .clone() should no longer be required.