Skip to content

Commit 9d08a93

Browse files
committed
---
yaml --- r: 38526 b: refs/heads/incoming c: 1a885f6 h: refs/heads/master v: v3
1 parent 848f413 commit 9d08a93

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278
9-
refs/heads/incoming: 2bb141c1f9c486442e57d255b4a7afafc9e89667
9+
refs/heads/incoming: 1a885f6be13e5ad241e4597a13e919803942e65b
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/rust.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,13 +1975,47 @@ An example of a call expression:
19751975
let x: int = add(1, 2);
19761976
~~~~
19771977

1978-
### Shared function expressions
1978+
### Lambda expressions
19791979

1980-
*TODO*.
1980+
~~~~~~~~ {.abnf .gram}
1981+
ident_list : [ ident [ ',' ident ]* ] ? ;
1982+
lambda_expr : '|' ident_list '| expr ;
1983+
~~~~~~~~
19811984

1982-
### Unique function expressions
1985+
A _lambda expression_ (a.k.a. "anonymous function expression") defines a function and denotes it as a value,
1986+
in a single expression.
1987+
Lambda expressions are written by prepending a list of identifiers, surrounded by pipe symbols (`|`),
1988+
to an expression.
19831989

1984-
*TODO*.
1990+
A lambda expression denotes a function mapping parameters to the expression to the right of the `ident_list`.
1991+
The identifiers in the `ident_list` are the parameters to the function, with types inferred from context.
1992+
1993+
Lambda expressions are most useful when passing functions as arguments to other functions,
1994+
as an abbreviation for defining and capturing a separate fucntion.
1995+
1996+
Significantly, lambda expressions _capture their environment_,
1997+
which regular [function definitions](#functions) do not.
1998+
1999+
The exact type of capture depends on the [function type](#function-types) inferred for the lambda expression;
2000+
in the simplest and least-expensive form, the environment is captured by reference,
2001+
effectively borrowing pointers to all outer variables referenced inside the function.
2002+
Other forms of capture include making copies of captured variables,
2003+
and moving values from the environment into the lambda expression's captured environment.
2004+
2005+
An example of a lambda expression:
2006+
2007+
~~~~
2008+
fn ten_times(f: fn(int)) {
2009+
let mut i = 0;
2010+
while i < 10 {
2011+
f(i);
2012+
i += 1;
2013+
}
2014+
}
2015+
2016+
ten_times(|j| io::println(fmt!("hello, %d", j)));
2017+
2018+
~~~~
19852019

19862020
### While loops
19872021

0 commit comments

Comments
 (0)