Skip to content

Commit faa11a7

Browse files
committed
DOC: Switch unfold example around, use closure captures
Show another way to use unfold, with the state in the closure captures. I think this shows that the explicit state formulation is redundant.
1 parent cdd0c1e commit faa11a7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/sources.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,18 @@ impl<A, F> Iterator for RepeatCall<F>
7070
///
7171
/// use itertools::unfold;
7272
///
73-
/// let mut fibonacci = unfold((1_u32, 1_u32), |state| {
74-
/// let (ref mut x1, ref mut x2) = *state;
75-
///
73+
/// let (mut x1, mut x2) = (1u32, 1u32);
74+
/// let mut fibonacci = unfold((), move |_| {
7675
/// // Attempt to get the next Fibonacci number
77-
/// let next = x1.saturating_add(*x2);
76+
/// let next = x1.saturating_add(x2);
7877
///
7978
/// // Shift left: ret <- x1 <- x2 <- next
80-
/// let ret = *x1;
81-
/// *x1 = *x2;
82-
/// *x2 = next;
79+
/// let ret = x1;
80+
/// x1 = x2;
81+
/// x2 = next;
8382
///
8483
/// // If addition has saturated at the maximum, we are finished
85-
/// if ret == *x1 && ret > 1 {
84+
/// if ret == x1 && ret > 1 {
8685
/// return None;
8786
/// }
8887
///

0 commit comments

Comments
 (0)