about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-12-20 16:51:37 -0800
committerBrian Anderson <banderson@mozilla.com>2012-12-20 16:51:37 -0800
commit45e62d0a144f336e2ad849c1bb7767a12f312643 (patch)
treeb99e4b64fa6e21acbe6178a1042d187f0d8a7c51
parentd098faa8555d48a9bc1de34542fc6d1dd5a2ab9c (diff)
tutorial: Discuss argument patterns
-rw-r--r--doc/tutorial.md24
1 files changed, 24 insertions, 0 deletions
diff --git a/doc/tutorial.md b/doc/tutorial.md
index e9ed83c8c73..3a90b0baacd 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -813,6 +813,15 @@ assert 8  == line(5, 3, 1);
 assert () == oops(5, 3, 1);
 ~~~~
 
+As with `match` expressions and `let` bindings, function arguments support
+pattern destructuring. Like `let`, argument patterns must be irrefutable,
+as in this example that unpacks a tuple and returns it.
+
+~~~
+fn first((value, _): (int, float)) -> int { value }
+~~~
+
+
 # The Rust memory model
 
 At this junction, let's take a detour to explain the concepts involved
@@ -1576,6 +1585,21 @@ fn contains(v: &[int], elt: int) -> bool {
 }
 ~~~~
 
+Notice that, because `each` passes each value by borrowed pointer,
+the iteratee needs to dereference it before using.
+In these situations it can be convenient to lean on Rust's
+argument patterns to bind `x` to the actual value, not the pointer.
+
+~~~~
+# use each = vec::each;
+# fn contains(v: &[int], elt: int) -> bool {
+    for each(v) |&x| {
+        if (x == elt) { return true; }
+    }
+#    false
+# }
+~~~~
+
 `for` syntax only works with stack closures.
 
 > ***Note:*** This is, essentially, a special loop protocol: