about summary refs log tree commit diff
path: root/doc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-12-02 23:32:33 -0800
committerbors <bors@rust-lang.org>2013-12-02 23:32:33 -0800
commitf1ef36ea2ffcb3af7ea76b323f8cb47670fd243c (patch)
treeb4e93cb2f1a16f3569b372acc360127eb8594613 /doc
parent18084be3164d2fe7c540662ea1e44cf73af9bf0f (diff)
parent94c02af8733cf369d21071d23f9bd7b249d4af8f (diff)
downloadrust-f1ef36ea2ffcb3af7ea76b323f8cb47670fd243c.tar.gz
rust-f1ef36ea2ffcb3af7ea76b323f8cb47670fd243c.zip
auto merge of #10773 : jvns/rust/patch-1, r=cmr
The section on closure types was missing, so I added one. I'm new to Rust, so there are probably important things to say about closure types that I'm missing here.

I tested the example with the latest Rust nightly.
Diffstat (limited to 'doc')
-rw-r--r--doc/rust.md26
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/rust.md b/doc/rust.md
index 92d9cb3ae38..7368ba2b7e7 100644
--- a/doc/rust.md
+++ b/doc/rust.md
@@ -3225,6 +3225,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.