rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.69k stars 303 forks source link

`iterate`: document a possible panic #842

Closed Philippe-Cholet closed 8 months ago

Philippe-Cholet commented 8 months ago

Resolves #600. See https://github.com/rust-itertools/itertools/issues/600#issuecomment-1884539907 for why I don't see an alternative.

Philippe-Cholet commented 8 months ago

Without Clone-able values, iterate and successors are forced to compute it ahead-of-time while we still have access to the value, it's the trade-off: not cloned but ahead-of-time computation. But being able to return None in successors' closure seems a better API decision to me.

EDIT: Not being able to stop the iterator with None kinda suppose it can continue forever (Iterate::size_hint explicitly says it).

The working example of iterate seems more like a successors example to me. iterate(1, |n| (n + 1) % 3) would be appropriate to me.

... It lead me to change the working example to better describe iterate vs successors. It could be something different than basically (1..=3).cycle() though.