From 36f98fb0bb9875bb7901c5e0cc757fde6ac0acdb Mon Sep 17 00:00:00 2001 From: mdinger Date: Sat, 19 Apr 2014 14:36:53 -0400 Subject: [PATCH] Demonstrate accessing external variable in first example --- src/doc/tutorial.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index ed70df7f3d37..6eee1a4d49a2 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -1717,27 +1717,32 @@ environment (sometimes referred to as "capturing" variables in their environment). For example, you couldn't write the following: ~~~~ {.ignore} -let foo = 10; +let x = 3; -// `bar` cannot refer to `foo` -fn bar() -> () { println!("{}", foo); } +// `fun` cannot refer to `x` +fn fun() -> () { println!("{}", x); } ~~~~ Rust also supports _closures_, functions that can access variables in -the enclosing scope. Compare `foo` in these: +the enclosing scope. Compare `x` in these: ~~~~ -fn bar() -> () { println!("{}", foo) }; // cannot reach enclosing scope -let closure = |foo| -> () { println!("{}", foo) }; // can reach enclosing scope +let x = 3; + +// `fun` is an invalid definition +fn fun () -> () { println!("{}", x) }; // cannot reach enclosing scope +let closure = || -> () { println!("{}", x) }; // can reach enclosing scope + +fun(); // Still won't work +closure(); // Prints: 3 ~~~~ Closures can be utilized in this fashion: ~~~~ -// Create a nameless function and assign it to `closure`. -// It's sole argument is a yet unknown `foo` to be supplied -// by the caller. -let closure = |foo| -> () { println!("{}", foo) }; +// Create a nameless function and assign it to `closure`. It's sole +// argument is a yet unknown `x` to be supplied by the caller. +let closure = |x| -> () { println!("{}", x) }; // Define `call_closure_with_ten` to take one argument and return null `()`. // `fun` is a function which takes one `int` argument `|int|` and also returns @@ -1752,7 +1757,7 @@ call_closure_with_ten(closure); This can be simplified by removing null arguments: ~~~~ -let closure = |foo| println!("{}", foo); +let closure = |x| println!("{}", x); fn call_closure_with_ten(fun: |int|) { fun(10); } call_closure_with_ten(closure);