I am wondering.
what is 'fuse'? why so need here?
It is documented.
Doc:
A stream that yields None
forever after the underlying stream yields None
once.
Iterators aren't necessarily expected to exhibit correct behavior if next
calls are made after already returning None
once, so, if one is given an arbitrary iterator and wants to be able to call it after it's already returned None
once, they can use fuse
which will guarantee that it just keeps returning None
and doesn't have other side effects
For more of a why you'd use it answer:
If an iterator isn't fuse, to consume it correctly you have to make sure that you always stop when something says it hit the end.
So something like let _ = it.nth(10); … use it …
is only correct if it
is fused -- if it might not be fused, you need to check for an Option
from nth
saying that it's exhausted, and make sure not to use it further.
Thus if you're accepting something where you're not sure whether it's fused and don't want to be careful about stuff like that, then you call .fuse()
on it to get a fused iterator and you can just call whatever since it'll stay exhausted once exhausted.
"Correctly" if the iteratator wasn't designed to be reused anyway (which, granted, is most of them).
But what example can there be where someone might call .next()
after a None
, and thus needs the iterator to be fused, just in case?
The simplest example is something like chain
-- it needs to give you something from the second iterator when the first one is exhausted. If the first iterator is fused, that's easily done by just first.next().or_else(|| second.next())
.
I think it's beneficial for optimizations in some cases. At least, there are or have been specialized implementations in the standard library based on Fuse
.
You may also run into non-ideally implemented iterators; that one occasionally gives unexpected results on release or panics on debug.
But then doesn't nth have to check n-1th etc? Wouldn't it be better to store a reference of which iter is the current iter?
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.