Skip to content

Commit 04b777c

Browse files
committed
---
yaml --- r: 115424 b: refs/heads/try c: b580964 h: refs/heads/master v: v3
1 parent eba69c9 commit 04b777c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: bee4e6adac17f87b1cdc26ab69f8c0f5d82575a3
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: ec0258a381b88b5574e3f8ce72ae553ac3a574b7
5-
refs/heads/try: ad0cdd7081bf78c4b7a00344f6f2b2f113cf0957
5+
refs/heads/try: b5809644ad6ee87dc28afd8200fa8e12213f4f29
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/doc/tutorial.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,19 +1719,41 @@ environment). For example, you couldn't write the following:
17191719
~~~~ {.ignore}
17201720
let foo = 10;
17211721
1722-
fn bar() -> int {
1723-
return foo; // `bar` cannot refer to `foo`
1724-
}
1722+
// `bar` cannot refer to `foo`
1723+
fn bar() -> () { println!("{}", foo); }
17251724
~~~~
17261725
17271726
Rust also supports _closures_, functions that can access variables in
1728-
the enclosing scope.
1727+
the enclosing scope. Compare `foo` in these:
1728+
1729+
~~~~
1730+
fn bar() -> () { println!("{}", foo) }; // cannot reach enclosing scope
1731+
let closure = |foo| -> () { println!("{}", foo) }; // can reach enclosing scope
1732+
~~~~
1733+
1734+
Closures can be utilized in this fashion:
17291735
17301736
~~~~
1731-
fn call_closure_with_ten(b: |int|) { b(10); }
1737+
// Create a nameless function and assign it to `closure`.
1738+
// It's sole argument is a yet unknown `foo` to be supplied
1739+
// by the caller.
1740+
let closure = |foo| -> () { println!("{}", foo) };
1741+
1742+
// Define `call_closure_with_ten` to take one argument and return null `()`.
1743+
// `fun` is a function which takes one `int` argument `|int|` and also returns
1744+
// null `()`. `|int|` defines the `fun` to be of type _closure_
1745+
fn call_closure_with_ten(fun: |int| -> ()) -> () { fun(10); }
17321746
1733-
let captured_var = 20;
1734-
let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
1747+
// The caller supplies `10` to the closure
1748+
// which prints out the value
1749+
call_closure_with_ten(closure);
1750+
~~~~
1751+
1752+
This can be simplified by removing null arguments:
1753+
1754+
~~~~
1755+
let closure = |foo| println!("{}", foo);
1756+
fn call_closure_with_ten(fun: |int|) { fun(10); }
17351757
17361758
call_closure_with_ten(closure);
17371759
~~~~

0 commit comments

Comments
 (0)