From d708452e86d528dca28d8ba6218c4f39a964baae Mon Sep 17 00:00:00 2001 From: "Sunrin SHIMURA (keen)" <3han5chou7@gmail.com> Date: Mon, 12 Dec 2016 20:35:34 +0900 Subject: [PATCH] update closures --- 1.9/ja/book/closures.md | 19 ++++--- diff-1.6.0..1.9.0/src/doc/book/closures.md | 63 ---------------------- 2 files changed, 9 insertions(+), 73 deletions(-) delete mode 100644 diff-1.6.0..1.9.0/src/doc/book/closures.md diff --git a/1.9/ja/book/closures.md b/1.9/ja/book/closures.md index 7b2775fe..529bc8d6 100644 --- a/1.9/ja/book/closures.md +++ b/1.9/ja/book/closures.md @@ -267,11 +267,11 @@ assert_eq!(5, num); - + Rustにおけるクロージャの実装は他の言語とは少し異なります。 Rustにおけるクロージャは実質的にトレイトへの糖衣構文です。 -続きの説明を読む前に [トレイト][traits] や [トレイトオブジェクト][trait-objects] についてのチャプターを学ぶ前に読みたくなるでしょう。 +続きの説明を読む前に [トレイト][traits] や [トレイトオブジェクト][trait-objects] についてのセクションを学ぶ前に読みたくなるでしょう。 [traits]: traits.html [trait-objects]: trait-objects.html @@ -330,9 +330,9 @@ Rustは環境用の構造体を作成し、 適切なトレイトを `impl` し # クロージャを引数に取る - + クロージャが実際にはトレイトであることを学んだので、 -クロージャを引数としたり返り値としたりする方法を既に知っていることになります: 通常のトレイトと同様に行うのです! +クロージャを引数としたり返り値としたりする方法を既に知っていることになります: 通常のトレイトと全く同様に行うのです! @@ -352,7 +352,7 @@ let answer = call_with_one(|x| x + 2); assert_eq!(3, answer); ``` - + クロージャ `|x| x + 2` を `call_with_one` に渡しました。 `call_with_one` はその関数名から推測される処理を行います: クロージャに `1` を与えて呼び出します。 @@ -479,14 +479,13 @@ assert_eq!(6, answer); このコードは以下の長いエラーを発生させます: ```text -error: the trait `core::marker::Sized` is not implemented for the type -`core::ops::Fn(i32) -> i32` [E0277] +error: the trait bound `core::ops::Fn(i32) -> i32 : core::marker::Sized` is not satisfied [E0277] fn factory() -> (Fn(i32) -> i32) { ^~~~~~~~~~~~~~~~ note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time fn factory() -> (Fn(i32) -> i32) { ^~~~~~~~~~~~~~~~ -error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn(i32) -> i32` [E0277] +error: the trait bound `core::ops::Fn(i32) -> i32 : core::marker::Sized` is not satisfied [E0277] let f = factory(); ^ note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time @@ -570,7 +569,7 @@ error: mismatched types: - + それぞれのクロージャはそれぞれの環境用の `struct` を生成し、 @@ -642,7 +641,7 @@ assert_eq!(6, answer); ``` - + `factory()` 内のクロージャを `move Fn` にすることで、新しいスタックフレームをクロージャのために生成します。 そしてボックス化することによって、既知のサイズとなり、現在のスタックフレームから抜けることが可能になります。 diff --git a/diff-1.6.0..1.9.0/src/doc/book/closures.md b/diff-1.6.0..1.9.0/src/doc/book/closures.md deleted file mode 100644 index ea142eb4..00000000 --- a/diff-1.6.0..1.9.0/src/doc/book/closures.md +++ /dev/null @@ -1,63 +0,0 @@ ---- a/src/doc/book/closures.md -+++ b/src/doc/book/closures.md -@@ -208,7 +208,7 @@ different. - - Rust’s implementation of closures is a bit different than other languages. They - are effectively syntax sugar for traits. You’ll want to make sure to have read --the [traits chapter][traits] before this one, as well as the chapter on [trait -+the [traits][traits] section before this one, as well as the section on [trait - objects][trait-objects]. - - [traits]: traits.html -@@ -253,7 +253,7 @@ use it. - # Taking closures as arguments - - Now that we know that closures are traits, we already know how to accept and --return closures: just like any other trait! -+return closures: the same as any other trait! - - This also means that we can choose static vs dynamic dispatch as well. First, - let’s write a function which takes something callable, calls it, and returns -@@ -271,7 +271,7 @@ let answer = call_with_one(|x| x + 2); - assert_eq!(3, answer); - ``` - --We pass our closure, `|x| x + 2`, to `call_with_one`. It just does what it -+We pass our closure, `|x| x + 2`, to `call_with_one`. It does what it - suggests: it calls the closure, giving it `1` as an argument. - - Let’s examine the signature of `call_with_one` in more depth: -@@ -371,14 +371,13 @@ assert_eq!(6, answer); - This gives us these long, related errors: - - ```text --error: the trait `core::marker::Sized` is not implemented for the type --`core::ops::Fn(i32) -> i32` [E0277] -+error: the trait bound `core::ops::Fn(i32) -> i32 : core::marker::Sized` is not satisfied [E0277] - fn factory() -> (Fn(i32) -> i32) { - ^~~~~~~~~~~~~~~~ - note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time - fn factory() -> (Fn(i32) -> i32) { - ^~~~~~~~~~~~~~~~ --error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn(i32) -> i32` [E0277] -+error: the trait bound `core::ops::Fn(i32) -> i32 : core::marker::Sized` is not satisfied [E0277] - let f = factory(); - ^ - note: `core::ops::Fn(i32) -> i32` does not have a constant size known at compile-time -@@ -448,7 +447,7 @@ This error is letting us know that we don’t have a `&'static Fn(i32) -> i32`, - we have a `[closure@:7:9: 7:20]`. Wait, what? - - Because each closure generates its own environment `struct` and implementation --of `Fn` and friends, these types are anonymous. They exist just solely for -+of `Fn` and friends, these types are anonymous. They exist solely for - this closure. So Rust shows them as `closure@`, rather than some - autogenerated name. - -@@ -502,5 +501,5 @@ assert_eq!(6, answer); - ``` - - By making the inner closure a `move Fn`, we create a new stack frame for our --closure. By `Box`ing it up, we’ve given it a known size, and allowing it to -+closure. By `Box`ing it up, we’ve given it a known size, allowing it to - escape our stack frame. -diff --git a/src/doc/book/compiler-plugins.md b/src/doc/book/compiler-plugins.md