auto merge of #10668 : vky/rust/closure-doc-update, r=alexcrichton
This commit is contained in:
commit
ef70b7666e
|
@ -1817,10 +1817,10 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~ {.xfail-test}\n"
|
"~~~~ {.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"
|
" for elt in seq.iter() { f(elt); }\n"
|
||||||
"}\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"
|
" let mut acc = ~[];\n"
|
||||||
" for elt in seq.iter() { acc.push(f(elt)); }\n"
|
" for elt in seq.iter() { acc.push(f(elt)); }\n"
|
||||||
" acc\n"
|
" acc\n"
|
||||||
|
@ -2404,7 +2404,7 @@ msgid ""
|
||||||
"trait Seq<T> {\n"
|
"trait Seq<T> {\n"
|
||||||
" fn len(&self) -> uint;\n"
|
" fn len(&self) -> uint;\n"
|
||||||
" fn elt_at(&self, n: uint) -> T;\n"
|
" fn elt_at(&self, n: uint) -> T;\n"
|
||||||
" fn iter(&self, &fn(T));\n"
|
" fn iter(&self, |T|);\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -4243,7 +4243,7 @@ msgid ""
|
||||||
"[function definitions](#functions) do not. The exact type of capture "
|
"[function definitions](#functions) do not. The exact type of capture "
|
||||||
"depends on the [function type](#function-types) inferred for the lambda "
|
"depends on the [function type](#function-types) inferred for the lambda "
|
||||||
"expression. In the simplest and least-expensive form (analogous to a "
|
"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 "
|
"by reference, effectively borrowing pointers to all outer variables "
|
||||||
"mentioned inside the function. Alternately, the compiler may infer that a "
|
"mentioned inside the function. Alternately, the compiler may infer that a "
|
||||||
"lambda expression should copy or move values (depending on their type.) "
|
"lambda expression should copy or move values (depending on their type.) "
|
||||||
|
@ -4262,7 +4262,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
"fn ten_times(f: &fn(int)) {\n"
|
"fn ten_times(f: |int|) {\n"
|
||||||
" let mut i = 0;\n"
|
" let mut i = 0;\n"
|
||||||
" while i < 10 {\n"
|
" while i < 10 {\n"
|
||||||
" f(i);\n"
|
" f(i);\n"
|
||||||
|
@ -4455,7 +4455,7 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/rust.md:2339
|
#: doc/rust.md:2339
|
||||||
msgid "~~~~ # fn f(f: &fn(int)) { } # fn g(i: int) { }"
|
msgid "~~~~ # fn f(f: |int|) { } # fn g(i: int) { }"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -4481,7 +4481,7 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/rust.md:2352
|
#: 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 ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -5483,7 +5483,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~~~~\n"
|
"~~~~~~~\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"
|
" if xs.len() == 0 {\n"
|
||||||
" return ~[];\n"
|
" return ~[];\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
|
|
@ -3340,10 +3340,10 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/tutorial.md:1434
|
#: 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 ""
|
msgstr ""
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
"fn call_closure_with_ten(b: &fn(int)) { b(10); }"
|
"fn call_closure_with_ten(b: |int|) { b(10); }"
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/tutorial.md:1437
|
#: doc/tutorial.md:1437
|
||||||
|
@ -3400,11 +3400,11 @@ msgstr ""
|
||||||
#: doc/tutorial.md:1459
|
#: doc/tutorial.md:1459
|
||||||
msgid ""
|
msgid ""
|
||||||
"There are several forms of closure, each with its own role. The most common, "
|
"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."
|
"variables in the enclosing scope."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"クロージャにはいくつかの形態があり、それぞれに独自の役割があります。最も一般"
|
"クロージャにはいくつかの形態があり、それぞれに独自の役割があります。最も一般"
|
||||||
"的なのはスタッククロージャと呼ばれるもので、 `&fn` という型を持ち、外側のロー"
|
"的なのはスタッククロージャと呼ばれるもので、 `||` という型を持ち、外側のロー"
|
||||||
"カル変数に直接アクセスすることができます。"
|
"カル変数に直接アクセスすることができます。"
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -3531,27 +3531,27 @@ msgstr "## クロージャの互換性"
|
||||||
msgid ""
|
msgid ""
|
||||||
"Rust closures have a convenient subtyping property: you can pass any kind of "
|
"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 "
|
"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 "
|
"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."
|
"pass any kind of closure."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Rust のクロージャは型の派生 (subtyping) という便利な性質を持っています。この"
|
"Rust のクロージャは型の派生 (subtyping) という便利な性質を持っています。この"
|
||||||
"性質により、`&fn()` 型を期待する関数には (引数と戻り値の型が一致する限り) 任"
|
"性質により、`||` 型を期待する関数には (引数と戻り値の型が一致する限り) 任"
|
||||||
"意の種類のクロージャを渡すことができます。したがって、引数で渡された関数につ"
|
"意の種類のクロージャを渡すことができます。したがって、引数で渡された関数につ"
|
||||||
"いては呼び出すだけで他に何もしない高階関数を書くときには、ほぼすべてのケース"
|
"いては呼び出すだけで他に何もしない高階関数を書くときには、ほぼすべてのケース"
|
||||||
"で引数の型を `&fn` と宣言するべきです。そうすることで、呼び出し元は任意の種類"
|
"で引数の型を `||` と宣言するべきです。そうすることで、呼び出し元は任意の種類"
|
||||||
"のクロージャを渡すことができるよになります。"
|
"のクロージャを渡すことができるよになります。"
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/tutorial.md:1527
|
#: doc/tutorial.md:1527
|
||||||
msgid ""
|
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 "
|
"closure, and it doesn't matter what type I am\"; }; fn function() { \"I'm a "
|
||||||
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
|
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
"let closure = || { \"I'm a closure, and it doesn't matter what type I am"
|
||||||
"\"; };\n"
|
"\"; };\n"
|
||||||
"fn function() { \"I'm a normal function\"; }\n"
|
"fn function() { \"I'm a normal function\"; }\n"
|
||||||
|
@ -3598,7 +3598,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
"fn each(v: &[int], op: &fn(v: &int)) {\n"
|
"fn each(v: &[int], op: |v: &int|) {\n"
|
||||||
" let mut n = 0;\n"
|
" let mut n = 0;\n"
|
||||||
" while n < v.len() {\n"
|
" while n < v.len() {\n"
|
||||||
" op(&v[n]);\n"
|
" op(&v[n]);\n"
|
||||||
|
@ -3622,7 +3622,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
"# fn do_some_work(i: &int) { }\n"
|
||||||
"each([1, 2, 3], |n| {\n"
|
"each([1, 2, 3], |n| {\n"
|
||||||
" do_some_work(n);\n"
|
" do_some_work(n);\n"
|
||||||
|
@ -3644,7 +3644,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
"# fn do_some_work(i: &int) { }\n"
|
||||||
"do each([1, 2, 3]) |n| {\n"
|
"do each([1, 2, 3]) |n| {\n"
|
||||||
" do_some_work(n);\n"
|
" do_some_work(n);\n"
|
||||||
|
@ -4011,7 +4011,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
" let mut accumulator = ~[];\n"
|
||||||
" for element in vector.iter() {\n"
|
" for element in vector.iter() {\n"
|
||||||
" accumulator.push(function(element));\n"
|
" accumulator.push(function(element));\n"
|
||||||
|
|
|
@ -1817,10 +1817,10 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~ {.xfail-test}\n"
|
"~~~~ {.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"
|
" for elt in seq.iter() { f(elt); }\n"
|
||||||
"}\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"
|
" let mut acc = ~[];\n"
|
||||||
" for elt in seq.iter() { acc.push(f(elt)); }\n"
|
" for elt in seq.iter() { acc.push(f(elt)); }\n"
|
||||||
" acc\n"
|
" acc\n"
|
||||||
|
@ -2404,7 +2404,7 @@ msgid ""
|
||||||
"trait Seq<T> {\n"
|
"trait Seq<T> {\n"
|
||||||
" fn len(&self) -> uint;\n"
|
" fn len(&self) -> uint;\n"
|
||||||
" fn elt_at(&self, n: uint) -> T;\n"
|
" fn elt_at(&self, n: uint) -> T;\n"
|
||||||
" fn iter(&self, &fn(T));\n"
|
" fn iter(&self, |T|);\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -4230,7 +4230,7 @@ msgid ""
|
||||||
"[function definitions](#functions) do not. The exact type of capture "
|
"[function definitions](#functions) do not. The exact type of capture "
|
||||||
"depends on the [function type](#function-types) inferred for the lambda "
|
"depends on the [function type](#function-types) inferred for the lambda "
|
||||||
"expression. In the simplest and least-expensive form (analogous to a "
|
"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 "
|
"by reference, effectively borrowing pointers to all outer variables "
|
||||||
"mentioned inside the function. Alternately, the compiler may infer that a "
|
"mentioned inside the function. Alternately, the compiler may infer that a "
|
||||||
"lambda expression should copy or move values (depending on their type.) "
|
"lambda expression should copy or move values (depending on their type.) "
|
||||||
|
@ -4249,7 +4249,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
"fn ten_times(f: &fn(int)) {\n"
|
"fn ten_times(f: |int|) {\n"
|
||||||
" let mut i = 0;\n"
|
" let mut i = 0;\n"
|
||||||
" while i < 10 {\n"
|
" while i < 10 {\n"
|
||||||
" f(i);\n"
|
" f(i);\n"
|
||||||
|
@ -4442,7 +4442,7 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/rust.md:2339
|
#: doc/rust.md:2339
|
||||||
msgid "~~~~ # fn f(f: &fn(int)) { } # fn g(i: int) { }"
|
msgid "~~~~ # fn f(f: |int|) { } # fn g(i: int) { }"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -4468,7 +4468,7 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/rust.md:2352
|
#: 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 ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -5470,7 +5470,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~~~~\n"
|
"~~~~~~~\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"
|
" if xs.len() == 0 {\n"
|
||||||
" return ~[];\n"
|
" return ~[];\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
|
|
@ -2558,7 +2558,7 @@ msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/tutorial.md:1434
|
#: 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 ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
|
@ -2601,7 +2601,7 @@ msgstr ""
|
||||||
#: doc/tutorial.md:1459
|
#: doc/tutorial.md:1459
|
||||||
msgid ""
|
msgid ""
|
||||||
"There are several forms of closure, each with its own role. The most common, "
|
"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."
|
"variables in the enclosing scope."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
@ -2700,16 +2700,16 @@ msgstr ""
|
||||||
msgid ""
|
msgid ""
|
||||||
"Rust closures have a convenient subtyping property: you can pass any kind of "
|
"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 "
|
"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 "
|
"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."
|
"pass any kind of closure."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#. type: Plain text
|
#. type: Plain text
|
||||||
#: doc/tutorial.md:1527
|
#: doc/tutorial.md:1527
|
||||||
msgid ""
|
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 "
|
"closure, and it doesn't matter what type I am\"; }; fn function() { \"I'm a "
|
||||||
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
|
"normal function\"; } call_twice(closure); call_twice(function); ~~~~"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -2746,7 +2746,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\n"
|
||||||
"fn each(v: &[int], op: &fn(v: &int)) {\n"
|
"fn each(v: &[int], op: |v: &int|) {\n"
|
||||||
" let mut n = 0;\n"
|
" let mut n = 0;\n"
|
||||||
" while n < v.len() {\n"
|
" while n < v.len() {\n"
|
||||||
" op(&v[n]);\n"
|
" op(&v[n]);\n"
|
||||||
|
@ -2768,7 +2768,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
"# fn do_some_work(i: &int) { }\n"
|
||||||
"each([1, 2, 3], |n| {\n"
|
"each([1, 2, 3], |n| {\n"
|
||||||
" do_some_work(n);\n"
|
" do_some_work(n);\n"
|
||||||
|
@ -2788,7 +2788,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
"# fn do_some_work(i: &int) { }\n"
|
||||||
"do each([1, 2, 3]) |n| {\n"
|
"do each([1, 2, 3]) |n| {\n"
|
||||||
" do_some_work(n);\n"
|
" do_some_work(n);\n"
|
||||||
|
@ -3080,7 +3080,7 @@ msgstr ""
|
||||||
#, no-wrap
|
#, no-wrap
|
||||||
msgid ""
|
msgid ""
|
||||||
"~~~~\n"
|
"~~~~\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"
|
" let mut accumulator = ~[];\n"
|
||||||
" for element in vector.iter() {\n"
|
" for element in vector.iter() {\n"
|
||||||
" accumulator.push(function(element));\n"
|
" accumulator.push(function(element));\n"
|
||||||
|
|
16
doc/rust.md
16
doc/rust.md
|
@ -950,10 +950,10 @@ declared, in an angle-bracket-enclosed, comma-separated list following
|
||||||
the function name.
|
the function name.
|
||||||
|
|
||||||
~~~~ {.xfail-test}
|
~~~~ {.xfail-test}
|
||||||
fn iter<T>(seq: &[T], f: &fn(T)) {
|
fn iter<T>(seq: &[T], f: |T|) {
|
||||||
for elt in seq.iter() { f(elt); }
|
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 = ~[];
|
let mut acc = ~[];
|
||||||
for elt in seq.iter() { acc.push(f(elt)); }
|
for elt in seq.iter() { acc.push(f(elt)); }
|
||||||
acc
|
acc
|
||||||
|
@ -1314,7 +1314,7 @@ These appear after the trait name, using the same syntax used in [generic functi
|
||||||
trait Seq<T> {
|
trait Seq<T> {
|
||||||
fn len(&self) -> uint;
|
fn len(&self) -> uint;
|
||||||
fn elt_at(&self, n: uint) -> T;
|
fn elt_at(&self, n: uint) -> T;
|
||||||
fn iter(&self, &fn(T));
|
fn iter(&self, |T|);
|
||||||
}
|
}
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
|
@ -2607,7 +2607,7 @@ as an abbreviation for defining and capturing a separate function.
|
||||||
Significantly, lambda expressions _capture their environment_,
|
Significantly, lambda expressions _capture their environment_,
|
||||||
which regular [function definitions](#functions) do not.
|
which regular [function definitions](#functions) do not.
|
||||||
The exact type of capture depends on the [function type](#function-types) inferred for the lambda expression.
|
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,
|
the lambda expression captures its environment by reference,
|
||||||
effectively borrowing pointers to all outer variables mentioned inside the function.
|
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.)
|
Alternately, the compiler may infer that a lambda expression should copy or move values (depending on their type.)
|
||||||
|
@ -2617,7 +2617,7 @@ In this example, we define a function `ten_times` that takes a higher-order func
|
||||||
and call it with a lambda expression as an argument.
|
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;
|
let mut i = 0;
|
||||||
while i < 10 {
|
while i < 10 {
|
||||||
f(i);
|
f(i);
|
||||||
|
@ -2726,7 +2726,7 @@ If the `expr` is a [field expression](#field-expressions), it is parsed as thoug
|
||||||
In this example, both calls to `f` are equivalent:
|
In this example, both calls to `f` are equivalent:
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
# fn f(f: &fn(int)) { }
|
# fn f(f: |int|) { }
|
||||||
# fn g(i: int) { }
|
# fn g(i: int) { }
|
||||||
|
|
||||||
f(|j| g(j));
|
f(|j| g(j));
|
||||||
|
@ -2739,7 +2739,7 @@ do f |j| {
|
||||||
In this example, both calls to the (binary) function `k` are equivalent:
|
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) { }
|
# fn l(i: int) { }
|
||||||
|
|
||||||
k(3, |j| l(j));
|
k(3, |j| l(j));
|
||||||
|
@ -3241,7 +3241,7 @@ and the cast expression in `main`.
|
||||||
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
|
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 {
|
if xs.len() == 0 {
|
||||||
return ~[];
|
return ~[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1366,7 +1366,7 @@ Rust also supports _closures_, functions that can access variables in
|
||||||
the enclosing scope.
|
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 captured_var = 20;
|
||||||
let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
|
let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
|
||||||
|
@ -1390,7 +1390,7 @@ let square = |x: int| -> uint { (x * x) as uint };
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
There are several forms of closure, each with its own role. The most
|
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.
|
access local variables in the enclosing scope.
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
|
@ -1420,13 +1420,13 @@ for spawning [tasks][tasks].
|
||||||
|
|
||||||
Rust closures have a convenient subtyping property: you can pass any kind of
|
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
|
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
|
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.
|
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"; };
|
let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
|
||||||
fn function() { "I'm a normal function"; }
|
fn function() { "I'm a normal function"; }
|
||||||
call_twice(closure);
|
call_twice(closure);
|
||||||
|
@ -1446,7 +1446,7 @@ Consider this function that iterates over a vector of
|
||||||
integers, passing in a pointer to each integer in the vector:
|
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;
|
let mut n = 0;
|
||||||
while n < v.len() {
|
while n < v.len() {
|
||||||
op(&v[n]);
|
op(&v[n]);
|
||||||
|
@ -1460,7 +1460,7 @@ argument, we can write it in a way that has a pleasant, block-like
|
||||||
structure.
|
structure.
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
# fn each(v: &[int], op: &fn(v: &int)) { }
|
# fn each(v: &[int], op: |v: &int|) { }
|
||||||
# fn do_some_work(i: &int) { }
|
# fn do_some_work(i: &int) { }
|
||||||
each([1, 2, 3], |n| {
|
each([1, 2, 3], |n| {
|
||||||
do_some_work(n);
|
do_some_work(n);
|
||||||
|
@ -1471,7 +1471,7 @@ This is such a useful pattern that Rust has a special form of function
|
||||||
call that can be written more like a built-in control structure:
|
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) { }
|
# fn do_some_work(i: &int) { }
|
||||||
do each([1, 2, 3]) |n| {
|
do each([1, 2, 3]) |n| {
|
||||||
do_some_work(n);
|
do_some_work(n);
|
||||||
|
@ -1650,7 +1650,7 @@ vector consisting of the result of applying `function` to each element
|
||||||
of `vector`:
|
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 = ~[];
|
let mut accumulator = ~[];
|
||||||
for element in vector.iter() {
|
for element in vector.iter() {
|
||||||
accumulator.push(function(element));
|
accumulator.push(function(element));
|
||||||
|
|
Loading…
Reference in New Issue