Skip to content

Commit

Permalink
Add doc example to PyIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Nov 2, 2018
1 parent 9ffb2c6 commit 52dfc00
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ use crate::types::PyObjectRef;
///
/// Unlike other python objects, this class includes a `Python<'p>` token
/// so that `PyIterator` can implement the rust `Iterator` trait.
///
/// # Example
///
/// ```rust
/// # use pyo3::prelude::*;
/// use pyo3::types::PyIterator;
///
/// # fn main() -> PyResult<()> {
/// let gil = Python::acquire_gil();
/// let py = gil.python();
/// let list = py.eval("iter([1, 2, 3, 4])", None, None)?;
/// let numbers: PyResult<Vec<usize>> = list.iter()?.map(|i| i.and_then(ObjectProtocol::extract::<usize>)).collect();
/// let sum: usize = numbers?.iter().sum();
/// assert_eq!(sum, 10);
/// # Ok(())
/// # }
/// ```
pub struct PyIterator<'p>(&'p PyObjectRef);

impl<'p> PyIterator<'p> {
Expand Down

0 comments on commit 52dfc00

Please sign in to comment.