container -- update example to contain scope of closure borrow
This commit is contained in:
parent
6f571a63a6
commit
0f5baad6ee
1 changed files with 19 additions and 13 deletions
|
@ -181,19 +181,25 @@ never call its underlying iterator again once `None` has been returned:
|
||||||
~~~
|
~~~
|
||||||
let xs = [1,2,3,4,5];
|
let xs = [1,2,3,4,5];
|
||||||
let mut calls = 0;
|
let mut calls = 0;
|
||||||
let it = xs.iter().scan((), |_, x| {
|
|
||||||
calls += 1;
|
{
|
||||||
if *x < 3 { Some(x) } else { None }});
|
let it = xs.iter().scan((), |_, x| {
|
||||||
// the iterator will only yield 1 and 2 before returning None
|
calls += 1;
|
||||||
// If we were to call it 5 times, calls would end up as 5, despite only 2 values
|
if *x < 3 { Some(x) } else { None }});
|
||||||
// being yielded (and therefore 3 unique calls being made). The fuse() adaptor
|
|
||||||
// can fix this.
|
// the iterator will only yield 1 and 2 before returning None
|
||||||
let mut it = it.fuse();
|
// If we were to call it 5 times, calls would end up as 5, despite
|
||||||
it.next();
|
// only 2 values being yielded (and therefore 3 unique calls being
|
||||||
it.next();
|
// made). The fuse() adaptor can fix this.
|
||||||
it.next();
|
|
||||||
it.next();
|
let mut it = it.fuse();
|
||||||
it.next();
|
it.next();
|
||||||
|
it.next();
|
||||||
|
it.next();
|
||||||
|
it.next();
|
||||||
|
it.next();
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(calls, 3);
|
assert_eq!(calls, 3);
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue