Skip to content

Commit 90dcb59

Browse files
committed
tutorial: Minor improvements to closures
1 parent 2ea8922 commit 90dcb59

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

doc/tutorial.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ let s = "a\
497497
498498
Rust's set of operators contains very few surprises. Binary arithmetic
499499
is done with `*`, `/`, `%`, `+`, and `-` (multiply, divide, remainder,
500-
plus, minus). `-` is also a unary prefix operator (there are no unary
501-
postfix operators in Rust) that does negation.
500+
plus, minus). `-` is also a unary prefix operator that does negation.
502501
503502
Binary shifting is done with `>>` (shift right), and `<<` (shift
504503
left). Shift right is arithmetic if the value is signed and logical if
@@ -909,8 +908,7 @@ returns it from a function, and then calls it:
909908
use std;
910909
911910
fn mk_appender(suffix: str) -> fn@(str) -> str {
912-
let f = fn@(s: str) -> str { s + suffix };
913-
ret f;
911+
ret fn@(s: str) -> str { s + suffix };
914912
}
915913
916914
fn main() {
@@ -932,6 +930,15 @@ fn mk_appender(suffix: str) -> fn@(str) -> str {
932930
}
933931
~~~~
934932
933+
### Unique closures
934+
935+
Unique closures, written `fn~` in analogy to the `~` pointer type (see
936+
next section), hold on to things that can safely be sent between
937+
processes. They copy the values they close over, much like boxed
938+
closures, but they also 'own' them—meaning no other code can access
939+
them. Unique closures are used in concurrent code, particularly
940+
for spawning [tasks](#tasks).
941+
935942
### Closure compatibility
936943
937944
A nice property of Rust closures is that you can pass any kind of
@@ -946,25 +953,17 @@ fn call_twice(f: fn()) { f(); f(); }
946953
call_twice(|| { "I am an inferred stack closure"; } );
947954
call_twice(fn&() { "I am also a stack closure"; } );
948955
call_twice(fn@() { "I am a boxed closure"; });
956+
call_twice(fn~() { "I am a unique closure"; });
949957
fn bare_function() { "I am a plain function"; }
950958
call_twice(bare_function);
951959
~~~~
952960
953-
### Unique closures
954-
955-
Unique closures, written `fn~` in analogy to the `~` pointer type (see
956-
next section), hold on to things that can safely be sent between
957-
processes. They copy the values they close over, much like boxed
958-
closures, but they also 'own' them—meaning no other code can access
959-
them. Unique closures are used in concurrent code, particularly
960-
for spawning [tasks](#tasks).
961-
962961
### Do syntax
963962
964-
Because closures in Rust are so versatile, they are used often, and in
965-
particular, functions taking closures are used as control structures
966-
in much the same way as `if` or `loop`. For example, this one iterates
967-
over a vector of integers backwards:
963+
Because closures in Rust are frequently used in combination with
964+
higher-order functions to simulate control structures like `if` and
965+
`loop`. For example, this one iterates over a vector of integers
966+
backwards:
968967
969968
~~~~
970969
fn for_rev(v: ~[int], act: fn(int)) {
@@ -976,11 +975,16 @@ fn for_rev(v: ~[int], act: fn(int)) {
976975
}
977976
~~~~
978977
979-
To run such an iteration, you could do this:
978+
To run such an iteration on a block of code, you could call
979+
it with a closure containing a block of code.
980980
981981
~~~~
982982
# fn for_rev(v: ~[int], act: fn(int)) {}
983-
for_rev(~[1, 2, 3], |n| log(error, n) );
983+
# fn do_some_work(i: int) { }
984+
for_rev(~[1, 2, 3], |n| {
985+
#debug("%i", n);
986+
do_some_work(n);
987+
});
984988
~~~~
985989
986990
Because this is such a common pattern Rust has a special form
@@ -989,8 +993,10 @@ structure:
989993
990994
~~~~
991995
# fn for_rev(v: [int], act: fn(int)) {}
996+
# fn do_some_work(i: int) { }
992997
do for_rev(~[1, 2, 3]) |n| {
993-
log(error, n);
998+
#debug("%i", n);
999+
do_some_work(n);
9941000
}
9951001
~~~~
9961002

0 commit comments

Comments
 (0)