Skip to content

Commit 3a28cb6

Browse files
bors[bot]Erutuon
andauthored
Merge #466
466: modify example of unfold so that it uses the initial_state value r=jswrenn a=Erutuon The example of `itertools::unfold` could easily be written with `std::iter::from_fn` because it ignores `initial_value` and uses captured variables instead: ```rust let (mut x1, mut x2) = (1u32, 1u32); let mut fibonacci = std::iter::from_fn(move || { // Attempt to get the next Fibonacci number let next = x1.saturating_add(x2); // Shift left: ret <- x1 <- x2 <- next let ret = x1; x1 = x2; x2 = next; // If addition has saturated at the maximum, we are finished if ret == x1 && ret > 1 { return None; } Some(ret) }); itertools::assert_equal(fibonacci.by_ref().take(8), vec![1, 1, 2, 3, 5, 8, 13, 21]); assert_eq!(fibonacci.last(), Some(2_971_215_073)) ``` `initial_value` is basically the whole point of `itertools::unfold`, the thing that distinguishes it from `std::iter::from_fn`, so I rewrote the example. Co-authored-by: Erutuon <[email protected]>
2 parents 600b91c + add669c commit 3a28cb6

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/sources.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,21 @@ impl<A, F> Iterator for RepeatCall<F>
7676
///
7777
/// use itertools::unfold;
7878
///
79-
/// let (mut x1, mut x2) = (1u32, 1u32);
80-
/// let mut fibonacci = unfold((), move |_| {
79+
/// let mut fibonacci = unfold((1u32, 1u32), |(x1, x2)| {
8180
/// // Attempt to get the next Fibonacci number
82-
/// let next = x1.saturating_add(x2);
81+
/// let next = x1.saturating_add(*x2);
8382
///
8483
/// // Shift left: ret <- x1 <- x2 <- next
85-
/// let ret = x1;
86-
/// x1 = x2;
87-
/// x2 = next;
84+
/// let ret = *x1;
85+
/// *x1 = *x2;
86+
/// *x2 = next;
8887
///
8988
/// // If addition has saturated at the maximum, we are finished
90-
/// if ret == x1 && ret > 1 {
91-
/// return None;
89+
/// if ret == *x1 && ret > 1 {
90+
/// None
91+
/// } else {
92+
/// Some(ret)
9293
/// }
93-
///
94-
/// Some(ret)
9594
/// });
9695
///
9796
/// itertools::assert_equal(fibonacci.by_ref().take(8),

0 commit comments

Comments
 (0)