Crates.io | unfold |
lib.rs | unfold |
version | 0.2.0 |
source | src |
created_at | 2021-01-14 20:25:41.38382 |
updated_at | 2021-01-18 20:34:17.884997 |
description | A simple unfold implementation in Rust |
homepage | |
repository | https://github.com/FilippoRanza/unfold/ |
max_upload_size | |
id | 342090 |
size | 8,134 |
A simple unfold implementation in Rust
unfold let you create an iterator that, staring from a given initial value, applies a given function to the current state, store the result for the next iteration, and return the current state.
Unfold defines an endless iterator: you must stop it by hand, like in the example.
use unfold::Unfold;
// Create a vector containing the first 5 numbers from the Fibonacci
// series
let fibonacci_numbers: Vec<u64> = Unfold::new(|(a, b)| (b, a + b), (0, 1))
.map(|(a, _)| a)
.take(5) //Unfold iterator never stops.
.collect();
assert_eq!(vec![0, 1, 1, 2, 3], fibonacci_numbers);