TryNext

Trait TryNext 

Source
pub trait TryNext {
    type Item;
    type Error;

    // Required method
    fn try_next(&mut self) -> Result<Option<Self::Item>, Self::Error>;

    // Provided method
    fn try_collect(&mut self) -> Result<Vec<Self::Item>, Self::Error> { ... }
}
Expand description

Context-free, fallible producer.

A trait for types that can produce items one at a time, where fetching the next item may fail.

This trait is synchronous — each call to try_next blocks until an item is produced or an error occurs. See the module-level documentation for details and examples.

Required Associated Types§

Source

type Item

The type of items yielded by this source.

Source

type Error

The error type that may be returned when producing the next item fails.

Required Methods§

Source

fn try_next(&mut self) -> Result<Option<Self::Item>, Self::Error>

Attempts to produce the next item from the source.

Returns:

  • Ok(Some(item)) — a new item was produced,
  • Ok(None) — the source is exhausted,
  • Err(e) — iteration failed with an error.

Provided Methods§

Source

fn try_collect(&mut self) -> Result<Vec<Self::Item>, Self::Error>

Collects all remaining items into a Vec.

The method repeatedly calls try_next until None or an error is returned, collecting all successful items into a vector.
If any call returns Err(e), iteration stops immediately and that error is returned.

§Feature

This method is only available when the alloc feature is enabled.

Implementors§