#, no-wrap
msgid ""
"~~~~ {.xfail-test}\n"
-"fn iter<T>(seq: &[T], f: &fn(T)) {\n"
+"fn iter<T>(seq: &[T], f: |T|) {\n"
" for elt in seq.iter() { f(elt); }\n"
"}\n"
-"fn map<T, U>(seq: &[T], f: &fn(T) -> U) -> ~[U] {\n"
+"fn map<T, U>(seq: &[T], f: |T| -> U) -> ~[U] {\n"
" let mut acc = ~[];\n"
" for elt in seq.iter() { acc.push(f(elt)); }\n"
" acc\n"
"trait Seq<T> {\n"
" fn len(&self) -> uint;\n"
" fn elt_at(&self, n: uint) -> T;\n"
-" fn iter(&self, &fn(T));\n"
+" fn iter(&self, |T|);\n"
"}\n"
"~~~~\n"
msgstr ""
"[function definitions](#functions) do not. The exact type of capture "
"depends on the [function type](#function-types) inferred for the lambda "
"expression. In the simplest and least-expensive form (analogous to a "
-"```&fn() { }``` expression), the lambda expression captures its environment "
+"```|| { }``` expression), the lambda expression captures its environment "
"by reference, effectively borrowing pointers to all outer variables "
"mentioned inside the function. Alternately, the compiler may infer that a "
"lambda expression should copy or move values (depending on their type.) "
#, no-wrap
msgid ""
"~~~~\n"
-"fn ten_times(f: &fn(int)) {\n"
+"fn ten_times(f: |int|) {\n"
" let mut i = 0;\n"
" while i < 10 {\n"
" f(i);\n"
#. type: Plain text
#: doc/rust.md:2339
-msgid "~~~~ # fn f(f: &fn(int)) { } # fn g(i: int) { }"
+msgid "~~~~ # fn f(f: |int|) { } # fn g(i: int) { }"
msgstr ""
#. type: Plain text
#. type: Plain text
#: doc/rust.md:2352
-msgid "~~~~ # fn k(x:int, f: &fn(int)) { } # fn l(i: int) { }"
+msgid "~~~~ # fn k(x:int, f: |int|) { } # fn l(i: int) { }"
msgstr ""
#. type: Plain text
#, no-wrap
msgid ""
"~~~~~~~\n"
-"fn map<A: Clone, B: Clone>(f: &fn(A) -> B, xs: &[A]) -> ~[B] {\n"
+"fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> ~[B] {\n"
" if xs.len() == 0 {\n"
" return ~[];\n"
" }\n"
#. type: Plain text
#: doc/tutorial.md:1434
-msgid "~~~~ fn call_closure_with_ten(b: &fn(int)) { b(10); }"
+msgid "~~~~ fn call_closure_with_ten(b: |int|) { b(10); }"
msgstr ""
"~~~~\n"
-"fn call_closure_with_ten(b: &fn(int)) { b(10); }"
+"fn call_closure_with_ten(b: |int|) { b(10); }"
#. type: Plain text
#: doc/tutorial.md:1437
#: doc/tutorial.md:1459
msgid ""
"There are several forms of closure, each with its own role. The most common, "
-"called a _stack closure_, has type `&fn` and can directly access local "
+"called a _stack closure_, has type `||` and can directly access local "
"variables in the enclosing scope."
msgstr ""
"クロージャにはいくつかの形態があり、それぞれに独自の役割があります。最も一般"
-"的なのはスタッククロージャと呼ばれるもので、 `&fn` という型を持ち、外側のロー"
+"的なのはスタッククロージャと呼ばれるもので、 `||` という型を持ち、外側のロー"
"カル変数に直接アクセスすることができます。"
#. type: Plain text
msgid ""
"Rust closures have a convenient subtyping property: you can pass any kind of "
"closure (as long as the arguments and return types match) to functions that "
-"expect a `&fn()`. Thus, when writing a higher-order function that only calls "
+"expect a `||`. Thus, when writing a higher-order function that only calls "
"its function argument, and does nothing else with it, you should almost "
-"always declare the type of that argument as `&fn()`. That way, callers may "
+"always declare the type of that argument as `||`. That way, callers may "
"pass any kind of closure."
msgstr ""
"Rust のクロージャは型の派生 (subtyping) という便利な性質を持っています。この"
-"性質により、`&fn()` 型を期待する関数には (引数と戻り値の型が一致する限り) 任"
+"性質により、`||` 型を期待する関数には (引数と戻り値の型が一致する限り) 任"
"意の種類のクロージャを渡すことができます。したがって、引数で渡された関数につ"
"いては呼び出すだけで他に何もしない高階関数を書くときには、ほぼすべてのケース"
-"で引数の型を `&fn` と宣言するべきです。そうすることで、呼び出し元は任意の種類"
+"で引数の型を `||` と宣言するべきです。そうすることで、呼び出し元は任意の種類"
"のクロージャを渡すことができるよになります。"
#. type: Plain text
#: doc/tutorial.md:1527
msgid ""
-"~~~~ fn call_twice(f: &fn()) { f(); f(); } let closure = || { \"I'm a "
+"~~~~ fn call_twice(f: ||) { f(); f(); } let closure = || { \"I'm a "
"closure, and it doesn't matter what type I am\"; }; fn function() { \"I'm a "
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
msgstr ""
"~~~~\n"
-"fn call_twice(f: &fn()) { f(); f(); }\n"
+"fn call_twice(f: ||) { f(); f(); }\n"
"let closure = || { \"I'm a closure, and it doesn't matter what type I am"
"\"; };\n"
"fn function() { \"I'm a normal function\"; }\n"
#, no-wrap
msgid ""
"~~~~\n"
-"fn each(v: &[int], op: &fn(v: &int)) {\n"
+"fn each(v: &[int], op: |v: &int|) {\n"
" let mut n = 0;\n"
" while n < v.len() {\n"
" op(&v[n]);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"# fn each(v: &[int], op: &fn(v: &int)) { }\n"
+"# fn each(v: &[int], op: |v: &int|) { }\n"
"# fn do_some_work(i: &int) { }\n"
"each([1, 2, 3], |n| {\n"
" do_some_work(n);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"# fn each(v: &[int], op: &fn(v: &int)) { }\n"
+"# fn each(v: &[int], op: |v: &int|) { }\n"
"# fn do_some_work(i: &int) { }\n"
"do each([1, 2, 3]) |n| {\n"
" do_some_work(n);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {\n"
+"fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> ~[U] {\n"
" let mut accumulator = ~[];\n"
" for element in vector.iter() {\n"
" accumulator.push(function(element));\n"
#, no-wrap
msgid ""
"~~~~ {.xfail-test}\n"
-"fn iter<T>(seq: &[T], f: &fn(T)) {\n"
+"fn iter<T>(seq: &[T], f: |T|) {\n"
" for elt in seq.iter() { f(elt); }\n"
"}\n"
-"fn map<T, U>(seq: &[T], f: &fn(T) -> U) -> ~[U] {\n"
+"fn map<T, U>(seq: &[T], f: |T| -> U) -> ~[U] {\n"
" let mut acc = ~[];\n"
" for elt in seq.iter() { acc.push(f(elt)); }\n"
" acc\n"
"trait Seq<T> {\n"
" fn len(&self) -> uint;\n"
" fn elt_at(&self, n: uint) -> T;\n"
-" fn iter(&self, &fn(T));\n"
+" fn iter(&self, |T|);\n"
"}\n"
"~~~~\n"
msgstr ""
"[function definitions](#functions) do not. The exact type of capture "
"depends on the [function type](#function-types) inferred for the lambda "
"expression. In the simplest and least-expensive form (analogous to a "
-"```&fn() { }``` expression), the lambda expression captures its environment "
+"```|| { }``` expression), the lambda expression captures its environment "
"by reference, effectively borrowing pointers to all outer variables "
"mentioned inside the function. Alternately, the compiler may infer that a "
"lambda expression should copy or move values (depending on their type.) "
#, no-wrap
msgid ""
"~~~~\n"
-"fn ten_times(f: &fn(int)) {\n"
+"fn ten_times(f: |int|) {\n"
" let mut i = 0;\n"
" while i < 10 {\n"
" f(i);\n"
#. type: Plain text
#: doc/rust.md:2339
-msgid "~~~~ # fn f(f: &fn(int)) { } # fn g(i: int) { }"
+msgid "~~~~ # fn f(f: |int|) { } # fn g(i: int) { }"
msgstr ""
#. type: Plain text
#. type: Plain text
#: doc/rust.md:2352
-msgid "~~~~ # fn k(x:int, f: &fn(int)) { } # fn l(i: int) { }"
+msgid "~~~~ # fn k(x:int, f: |int|) { } # fn l(i: int) { }"
msgstr ""
#. type: Plain text
#, no-wrap
msgid ""
"~~~~~~~\n"
-"fn map<A: Clone, B: Clone>(f: &fn(A) -> B, xs: &[A]) -> ~[B] {\n"
+"fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> ~[B] {\n"
" if xs.len() == 0 {\n"
" return ~[];\n"
" }\n"
#. type: Plain text
#: doc/tutorial.md:1434
-msgid "~~~~ fn call_closure_with_ten(b: &fn(int)) { b(10); }"
+msgid "~~~~ fn call_closure_with_ten(b: |int|) { b(10); }"
msgstr ""
#. type: Plain text
#: doc/tutorial.md:1459
msgid ""
"There are several forms of closure, each with its own role. The most common, "
-"called a _stack closure_, has type `&fn` and can directly access local "
+"called a _stack closure_, has type `||` and can directly access local "
"variables in the enclosing scope."
msgstr ""
msgid ""
"Rust closures have a convenient subtyping property: you can pass any kind of "
"closure (as long as the arguments and return types match) to functions that "
-"expect a `&fn()`. Thus, when writing a higher-order function that only calls "
+"expect a `||`. Thus, when writing a higher-order function that only calls "
"its function argument, and does nothing else with it, you should almost "
-"always declare the type of that argument as `&fn()`. That way, callers may "
+"always declare the type of that argument as `||`. That way, callers may "
"pass any kind of closure."
msgstr ""
#. type: Plain text
#: doc/tutorial.md:1527
msgid ""
-"~~~~ fn call_twice(f: &fn()) { f(); f(); } let closure = || { \"I'm a "
+"~~~~ fn call_twice(f: ||) { f(); f(); } let closure = || { \"I'm a "
"closure, and it doesn't matter what type I am\"; }; fn function() { \"I'm a "
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
msgstr ""
#, no-wrap
msgid ""
"~~~~\n"
-"fn each(v: &[int], op: &fn(v: &int)) {\n"
+"fn each(v: &[int], op: |v: &int|) {\n"
" let mut n = 0;\n"
" while n < v.len() {\n"
" op(&v[n]);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"# fn each(v: &[int], op: &fn(v: &int)) { }\n"
+"# fn each(v: &[int], op: |v: &int|) { }\n"
"# fn do_some_work(i: &int) { }\n"
"each([1, 2, 3], |n| {\n"
" do_some_work(n);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"# fn each(v: &[int], op: &fn(v: &int)) { }\n"
+"# fn each(v: &[int], op: |v: &int|) { }\n"
"# fn do_some_work(i: &int) { }\n"
"do each([1, 2, 3]) |n| {\n"
" do_some_work(n);\n"
#, no-wrap
msgid ""
"~~~~\n"
-"fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {\n"
+"fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> ~[U] {\n"
" let mut accumulator = ~[];\n"
" for element in vector.iter() {\n"
" accumulator.push(function(element));\n"
the function name.
~~~~ {.xfail-test}
-fn iter<T>(seq: &[T], f: &fn(T)) {
+fn iter<T>(seq: &[T], f: |T|) {
for elt in seq.iter() { f(elt); }
}
-fn map<T, U>(seq: &[T], f: &fn(T) -> U) -> ~[U] {
+fn map<T, U>(seq: &[T], f: |T| -> U) -> ~[U] {
let mut acc = ~[];
for elt in seq.iter() { acc.push(f(elt)); }
acc
trait Seq<T> {
fn len(&self) -> uint;
fn elt_at(&self, n: uint) -> T;
- fn iter(&self, &fn(T));
+ fn iter(&self, |T|);
}
~~~~
Significantly, lambda expressions _capture their environment_,
which regular [function definitions](#functions) do not.
The exact type of capture depends on the [function type](#function-types) inferred for the lambda expression.
-In the simplest and least-expensive form (analogous to a ```&fn() { }``` expression),
+In the simplest and least-expensive form (analogous to a ```|| { }``` expression),
the lambda expression captures its environment by reference,
effectively borrowing pointers to all outer variables mentioned inside the function.
Alternately, the compiler may infer that a lambda expression should copy or move values (depending on their type.)
and call it with a lambda expression as an argument.
~~~~
-fn ten_times(f: &fn(int)) {
+fn ten_times(f: |int|) {
let mut i = 0;
while i < 10 {
f(i);
In this example, both calls to `f` are equivalent:
~~~~
-# fn f(f: &fn(int)) { }
+# fn f(f: |int|) { }
# fn g(i: int) { }
f(|j| g(j));
In this example, both calls to the (binary) function `k` are equivalent:
~~~~
-# fn k(x:int, f: &fn(int)) { }
+# fn k(x:int, f: |int|) { }
# fn l(i: int) { }
k(3, |j| l(j));
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
~~~~
-fn map<A: Clone, B: Clone>(f: &fn(A) -> B, xs: &[A]) -> ~[B] {
+fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> ~[B] {
if xs.len() == 0 {
return ~[];
}
the enclosing scope.
~~~~
-fn call_closure_with_ten(b: &fn(int)) { b(10); }
+fn call_closure_with_ten(b: |int|) { b(10); }
let captured_var = 20;
let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
~~~~
There are several forms of closure, each with its own role. The most
-common, called a _stack closure_, has type `&fn` and can directly
+common, called a _stack closure_, has type `||` and can directly
access local variables in the enclosing scope.
~~~~
Rust closures have a convenient subtyping property: you can pass any kind of
closure (as long as the arguments and return types match) to functions
-that expect a `&fn()`. Thus, when writing a higher-order function that
+that expect a `||`. Thus, when writing a higher-order function that
only calls its function argument, and does nothing else with it, you
-should almost always declare the type of that argument as `&fn()`. That way,
+should almost always declare the type of that argument as `||`. That way,
callers may pass any kind of closure.
~~~~
-fn call_twice(f: &fn()) { f(); f(); }
+fn call_twice(f: ||) { f(); f(); }
let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
fn function() { "I'm a normal function"; }
call_twice(closure);
integers, passing in a pointer to each integer in the vector:
~~~~
-fn each(v: &[int], op: &fn(v: &int)) {
+fn each(v: &[int], op: |v: &int|) {
let mut n = 0;
while n < v.len() {
op(&v[n]);
structure.
~~~~
-# fn each(v: &[int], op: &fn(v: &int)) { }
+# fn each(v: &[int], op: |v: &int|) { }
# fn do_some_work(i: &int) { }
each([1, 2, 3], |n| {
do_some_work(n);
call that can be written more like a built-in control structure:
~~~~
-# fn each(v: &[int], op: &fn(v: &int)) { }
+# fn each(v: &[int], op: |v: &int|) { }
# fn do_some_work(i: &int) { }
do each([1, 2, 3]) |n| {
do_some_work(n);
of `vector`:
~~~~
-fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
+fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> ~[U] {
let mut accumulator = ~[];
for element in vector.iter() {
accumulator.push(function(element));