W3cubDocs

/Rust

Function std::iter::successors

pub fn successors<T, F>(first: Option<T>, succ: F) -> Successors<T, F>ⓘNotable traits for Successors<T, F>impl<T, F> Iterator for Successors<T, F> where    F: FnMut(&T) -> Option<T>,     type Item = T; where    F: FnMut(&T) -> Option<T>, 

Creates a new iterator where each successive item is computed based on the preceding one.

The iterator starts with the given first item (if any) and calls the given FnMut(&T) -> Option<T> closure to compute each item’s successor.

use std::iter::successors;

let powers_of_10 = successors(Some(1_u16), |n| n.checked_mul(10));
assert_eq!(powers_of_10.collect::<Vec<_>>(), &[1, 10, 100, 1_000, 10_000]);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/iter/fn.successors.html