@@ -497,8 +497,7 @@ let s = "a\
497
497
498
498
Rust's set of operators contains very few surprises. Binary arithmetic
499
499
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.
502
501
503
502
Binary shifting is done with `>>` (shift right), and `<<` (shift
504
503
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:
909
908
use std;
910
909
911
910
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 };
914
912
}
915
913
916
914
fn main() {
@@ -932,6 +930,15 @@ fn mk_appender(suffix: str) -> fn@(str) -> str {
932
930
}
933
931
~~~~
934
932
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
+
935
942
### Closure compatibility
936
943
937
944
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(); }
946
953
call_twice(|| { "I am an inferred stack closure"; } );
947
954
call_twice(fn&() { "I am also a stack closure"; } );
948
955
call_twice(fn@() { "I am a boxed closure"; });
956
+ call_twice(fn~() { "I am a unique closure"; });
949
957
fn bare_function() { "I am a plain function"; }
950
958
call_twice(bare_function);
951
959
~~~~
952
960
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
-
962
961
### Do syntax
963
962
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:
968
967
969
968
~~~~
970
969
fn for_rev(v: ~[int], act: fn(int)) {
@@ -976,11 +975,16 @@ fn for_rev(v: ~[int], act: fn(int)) {
976
975
}
977
976
~~~~
978
977
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.
980
980
981
981
~~~~
982
982
# 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
+ });
984
988
~~~~
985
989
986
990
Because this is such a common pattern Rust has a special form
@@ -989,8 +993,10 @@ structure:
989
993
990
994
~~~~
991
995
# fn for_rev(v: [int], act: fn(int)) {}
996
+ # fn do_some_work(i: int) { }
992
997
do for_rev(~[1, 2, 3]) |n| {
993
- log(error, n);
998
+ #debug("%i", n);
999
+ do_some_work(n);
994
1000
}
995
1001
~~~~
996
1002
0 commit comments