diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2014-02-07 06:52:01 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2014-02-11 16:55:22 -0500 |
| commit | 0f5baad6ee5191991d18271aabebaf3fc6124424 (patch) | |
| tree | 962b21fa2ef5408adc39a69586ad0e80d2e10075 /src/doc | |
| parent | 6f571a63a6e966ae59e0d078542c04734eba58ac (diff) | |
| download | rust-0f5baad6ee5191991d18271aabebaf3fc6124424.tar.gz rust-0f5baad6ee5191991d18271aabebaf3fc6124424.zip | |
container -- update example to contain scope of closure borrow
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/guide-container.md | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/doc/guide-container.md b/src/doc/guide-container.md index 1c79be1eb82..ebad650a534 100644 --- a/src/doc/guide-container.md +++ b/src/doc/guide-container.md @@ -181,19 +181,25 @@ never call its underlying iterator again once `None` has been returned: ~~~ let xs = [1,2,3,4,5]; let mut calls = 0; -let it = xs.iter().scan((), |_, x| { - calls += 1; - if *x < 3 { Some(x) } else { None }}); -// the iterator will only yield 1 and 2 before returning None -// If we were to call it 5 times, calls would end up as 5, despite only 2 values -// being yielded (and therefore 3 unique calls being made). The fuse() adaptor -// can fix this. -let mut it = it.fuse(); -it.next(); -it.next(); -it.next(); -it.next(); -it.next(); + +{ + let it = xs.iter().scan((), |_, x| { + calls += 1; + if *x < 3 { Some(x) } else { None }}); + + // the iterator will only yield 1 and 2 before returning None + // If we were to call it 5 times, calls would end up as 5, despite + // only 2 values being yielded (and therefore 3 unique calls being + // made). The fuse() adaptor can fix this. + + let mut it = it.fuse(); + it.next(); + it.next(); + it.next(); + it.next(); + it.next(); +} + assert_eq!(calls, 3); ~~~ |
