File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -3193,6 +3193,32 @@ let bo: Binop = add;
3193
3193
x = bo(5,7);
3194
3194
~~~~
3195
3195
3196
+ ### Closure types
3197
+
3198
+ The type of a closure mapping an input of type ` A ` to an output of type ` B ` is ` |A| -> B ` . A closure with no arguments or return values has type ` || ` .
3199
+
3200
+
3201
+ An example of creating and calling a closure:
3202
+
3203
+ ``` rust
3204
+ let captured_var = 10 ;
3205
+
3206
+ let closure_no_args = || println! (" captured_var={}" , captured_var );
3207
+
3208
+ let closure_args = | arg : int | -> int {
3209
+ println! (" captured_var={}, arg={}" , captured_var , arg );
3210
+ arg // Note lack of semicolon after 'arg'
3211
+ };
3212
+
3213
+ fn call_closure (c1 : || , c2 : | int | -> int ) {
3214
+ c1 ();
3215
+ c2 (2 );
3216
+ }
3217
+
3218
+ call_closure (closure_no_args , closure_args );
3219
+
3220
+ ```
3221
+
3196
3222
### Object types
3197
3223
3198
3224
Every trait item (see [ traits] ( #traits ) ) defines a type with the same name as the trait.
You can’t perform that action at this time.
0 commit comments