Skip to content

Commit

Permalink
tutorial: Discuss argument patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Dec 21, 2012
1 parent d098faa commit 45e62d0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 45e62d0

Please sign in to comment.