about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJulia Evans <julia@jvns.ca>2013-12-02 14:39:11 -0500
committerJulia Evans <julia@jvns.ca>2013-12-02 14:39:11 -0500
commit94c02af8733cf369d21071d23f9bd7b249d4af8f (patch)
tree0098c1c98c63880049818fc2d1d69117cc9a0b64
parent61443dc1f5089df637edba83587b9f3020063266 (diff)
Add section on closure types to manual
-rw-r--r--doc/rust.md26
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/rust.md b/doc/rust.md
index 969e40e632a..d71fefa2dd7 100644
--- a/doc/rust.md
+++ b/doc/rust.md
@@ -3193,6 +3193,32 @@ let bo: Binop = add;
 x = bo(5,7);
 ~~~~
 
+### Closure types
+
+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 `||`.
+
+
+An example of creating and calling a closure:
+
+```rust
+let captured_var = 10; 
+
+let closure_no_args = || println!("captured_var={}", captured_var); 
+
+let closure_args = |arg: int| -> int {
+  println!("captured_var={}, arg={}", captured_var, arg); 
+  arg // Note lack of semicolon after 'arg'
+};
+
+fn call_closure(c1: ||, c2: |int| -> int) {
+  c1();
+  c2(2);
+}
+
+call_closure(closure_no_args, closure_args);
+
+```
+
 ### Object types
 
 Every trait item (see [traits](#traits)) defines a type with the same name as the trait.