From 7e226accb60edafe1c1bbd75d6e6f768db9b57f9 Mon Sep 17 00:00:00 2001 From: blueblade Date: Sat, 1 May 2021 21:44:41 +0900 Subject: [PATCH 01/10] translate some untranslated lines --- src/SUMMARY.md | 8 +++++- src/conversion.md | 6 +++++ src/conversion/from_into.md | 23 +++++++++++++++- src/custom_types/enum.md | 26 ++++++++++++++++++- src/custom_types/structs.md | 19 ++++++++++---- src/hello/print.md | 5 +++- src/hello/print/print_debug.md | 3 +++ .../print/print_display/testcase_list.md | 6 +++++ src/primitives.md | 22 ++++++++++++---- src/primitives/array.md | 10 ++++--- src/primitives/tuples.md | 2 ++ src/variable_bindings/freeze.md | 5 +++- 12 files changed, 116 insertions(+), 19 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 0582398eb..889d4f495 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -67,16 +67,22 @@ - [Inference](types/inference.md) - [Aliasing](types/alias.md) --> -- [Types](types.md) +- [型](types.md) - [型キャスティング](types/cast.md) - [リテラル](types/literals.md) - [型推論](types/inference.md) - [エイリアス](types/alias.md) + +- [型変換](conversion.md) + - [`From`および`Into`](conversion/from_into.md) + - [`TryFrom` and `TryInto`](conversion/try_from_try_into.md) + - [To and from `String`s](conversion/string.md) +# 型変換 + +Rustは型の変換を[トレイト][traits]を用いて行います。一般的な型変換には[`From`]および[`Into`]トレイトを使用します。しかし、よくあるケースにおいて、特に`String`との相互の型変換では、特殊なトレイトが使用されます。 [traits]: trait.md [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html diff --git a/src/conversion/from_into.md b/src/conversion/from_into.md index 266d10f28..058e76a8a 100644 --- a/src/conversion/from_into.md +++ b/src/conversion/from_into.md @@ -1,24 +1,39 @@ + + +# `From`および`Into` + +[`From`]トレイトと[`Into`]トレイトは本質的に結びついており、そのことが実際に実装に反映されています。もし型Aから型Bへの変換ができるのであれば、型Bから型Aへの変換もできると思うのが自然です。 ## `From` + +[`From`]トレイトは、ある型に対し、別の型からその型を作る方法を定義できるようにするものです。そのため、複数の型の間で型変換を行うための非常にシンプルな仕組みを提供しています。標準ライブラリでは、基本型やよく使われる型に対して、このトレイトが多数実装されています。 + +例えば、`str`から`String`への型変換は簡単です。 ```rust let my_str = "hello"; let my_string = String::from(my_str); ``` - + +自作の型に対しても、型変換を定義すれば同じように行えます。 ```rust,editable use std::convert::From; @@ -42,13 +57,19 @@ fn main() { ## `Into` + +[`Into`]トレイトは、単に`From`トレイトの逆の働きをします。もし自作の型に`From`トレイトが実装されていたら、`Into`は必要に応じてそれを呼び出します。 + +`Into`トレイトを使用すると、ほとんどの場合、コンパイラが型を決定することができないため、変換する型を指定する必要があります。しかし、この機能を無料で得られることを考えれば、これは小さなトレードオフです。 ```rust,editable use std::convert::From; diff --git a/src/custom_types/enum.md b/src/custom_types/enum.md index dd14b671c..0be4569a0 100644 --- a/src/custom_types/enum.md +++ b/src/custom_types/enum.md @@ -8,26 +8,35 @@ The `enum` keyword allows the creation of a type which may be one of a few different variants. Any variant which is valid as a `struct` is also valid as an `enum`. --> -列挙型(`enum`)はいくつかの異なる型の中から1つを選ぶような場合に使用する。構造体(`struct`)の定義を満たすものならば何でも`enum` 内の型として使用できる +列挙型(`enum`)はいくつかの異なる要素型の中から1つを選ぶような場合に使用します。構造体(`struct`)の定義を満たすものならば何でも`enum` の要素型として使用できます。 ```rust,editable // Create an `enum` to classify a web event. Note how both // names and type information together specify the variant: // `PageLoad != PageUnload` and `KeyPress(char) != Paste(String)`. // Each is different and independent. +// `enum`を作成してwebイベントを分類する。 +// 名前と型情報を併せたものが要素型になっていることに注意。 +// `PageLoad != PageUnload` であり、 +// `KeyPress(char) != Paste(String)` である。 +// 要素型は互いに異なり、互いに非依存である。 enum WebEvent { // An `enum` may either be `unit-like`, + // `enum`要素型はユニット風でもよい PageLoad, PageUnload, // like tuple structs, + // タプル風でもよい KeyPress(char), Paste(String), // or c-like structures. + // C言語スタイルの構造体風でもよい Click { x: i64, y: i64 }, } // A function which takes a `WebEvent` enum as an argument and // returns nothing. +// 引数として`WebEvent`列挙型をとり、何も返さない関数 fn inspect(event: WebEvent) { match event { WebEvent::PageLoad => println!("page loaded"), @@ -45,6 +54,7 @@ fn inspect(event: WebEvent) { fn main() { let pressed = WebEvent::KeyPress('x'); // `to_owned()` creates an owned `String` from a string slice. + // `to_owned()`は文字列スライスから所有権のある`String`を作成する let pasted = WebEvent::Paste("my text".to_owned()); let click = WebEvent::Click { x: 20, y: 80 }; let load = WebEvent::PageLoad; @@ -59,11 +69,17 @@ fn main() { ``` + +## 型エイリアス + +型エイリアスを用いると、列挙型の要素型を別名で参照できます。これは列挙型の名前があまりに長かったり、あまりに一般的だったりで改名したい場合に役立ちます。 ```rust,editable enum VeryVerboseEnumOfThingsToDoWithNumbers { @@ -72,16 +88,21 @@ enum VeryVerboseEnumOfThingsToDoWithNumbers { } // Creates a type alias +// 型エイリアスを作成する type Operations = VeryVerboseEnumOfThingsToDoWithNumbers; fn main() { // We can refer to each variant via its alias, not its long and inconvenient // name. + // 長くて不便な列挙型の名前ではなく、別名を使って要素型を参照できる let x = Operations::Add; } ``` + +このやり方がもっともよく見られるのは、`impl`ブロックで`Self`という別名を使用する場合です。 ```rust,editable enum VeryVerboseEnumOfThingsToDoWithNumbers { @@ -99,9 +120,12 @@ impl VeryVerboseEnumOfThingsToDoWithNumbers { } ``` + +列挙型や型エイリアスについて詳しく学びたい人は、この機能が安定してRustに取り込まれた後に[stabilization report][aliasreport]を読んでください。 -`struct`というキーワードを用いて作成できる構造体(「structre」)には3種類あります。 +`struct`というキーワードを用いて作成できる構造体(「structure」)には3種類あります。 -この例で用いられている型は、標準ライブラリに含まれているため、ここでは`fmt::Display`を使用しています。他の型の場合、もうちょっと複雑なステップが要求される場合があります。 +この例で用いられている型は、標準ライブラリに含まれているため、ここでは`fmt::Display`を使用しています。カスタム型をテキストとして表示する場合は、さらに手順が必要です。 + +`fmt::Display`トレイトを実装すると、自動的に[`ToString`]トレイトが実装されます。これにより[`String`][string]型への型変換ができるようになります。 +手動で`fmt::Display`を実装することでプリント結果を思い通りにできます。 +### 演習 + +上記のプログラムを変更して、ベクタの各要素のインデックスも表示するようにしてみましょう。変更後の出力は次のようになります。 ```rust,ignore [0: 1, 1: 2, 2: 3] diff --git a/src/primitives.md b/src/primitives.md index 2cbc1bbd8..24f96aa45 100644 --- a/src/primitives.md +++ b/src/primitives.md @@ -9,7 +9,10 @@ Rust provides access to a wide variety of `primitives`. A sample includes: Rustは様々な基本データ型(`primitives`)の使用をサポートしています。以下がその例です。 + +### スカラー型 * 符号付き整数: `i8`, `i16`, `i32`, `i64`, `i128`, `isize`(ポインタのサイズ) * 符号無し整数: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`(ポインタのサイズ) -* 浮動小数点: `f32`, `f64` -* `char`: `'a'`, `'α'`, `'∞'`などのUnicodeのスカラー +* 浮動小数点数: `f32`, `f64` +* `char`: `'a'`, `'α'`, `'∞'`などのUnicodeのスカラー値 * `bool`: `true`または`false` -* unit型: `()`が唯一の値 +* ユニット型: `()`が唯一の値 + +ユニット型はその値がタプルですが、複合型とはみなされません。内部に複数の値を含んでいるわけではないからです。 + +### 複合型 -変数は常に *型指定(`type annotate`)可能* です。数値型の場合はさらにサフィックスでの指定が可能です。指定しない場合デフォルトになります。例えば整数は`i32`が、浮動小数点は`f64`がデフォルトです。 -Note that Rust can also infer types from context. +変数は常に *型指定(`type annotate`)可能* です。数値型の場合はさらにサフィックスでの指定が可能です。指定しない場合デフォルトになります。例えば整数は`i32`が、浮動小数点は`f64`がデフォルトです。また、Rustは文脈から型を推定することもできます。 ```rust,editable,ignore,mdbook-runnable fn main() { @@ -64,10 +72,13 @@ fn main() { let default_integer = 7; // `i32` // A type can also be inferred from context + // 型を文脈から推定することも可能 let mut inferred_type = 12; // Type i64 is inferred from another line + // 型 i64 は次行の内容に基づいて推定 inferred_type = 4294967296i64; // A mutable variable's value can be changed. + // ミュータブルな変数は値を変更できる let mut mutable = 12; // Mutable `i32` // ミュータブルな `i32`. mutable = 21; @@ -77,6 +88,7 @@ fn main() { mutable = true; // Variables can be overwritten with shadowing. + // 変数はシャドーイングによって上書きできる let mutable = true; } ``` diff --git a/src/primitives/array.md b/src/primitives/array.md index a8deedbe0..f60a1f070 100644 --- a/src/primitives/array.md +++ b/src/primitives/array.md @@ -18,9 +18,7 @@ usize, determined by the processor architecture eg 64 bits on an x86-64. Slices can be used to borrow a section of an array, and have the type signature `&[T]`. --> -スライスは配列に似ていますが、コンパイル時にサイズが決定されていません。2つの部分からなり、1つは別のデータへのポインタ、もう1つはスライスの長さです。 -The word size is the same as usize, determined by the processor architecture eg 64 bits on an x86-64. -配列の一部を借用するのに使用され`&[T]`という型シグネチャを持ちます。 +スライスは配列に似ていますが、コンパイル時にサイズが決定されていません。スライスは2ワードからなるオブジェクトであり、最初のワードがデータへのポインタ、2番目のワードがスライスの長さです。ワード長は`usize`と同一で、プロセッサのアーキテクチャによって決まります。例えばx86-64では64ビットです。スライスは配列の一部を借用するのに使用され、`&[T]`という型シグネチャを持ちます。 ```rust,editable,ignore,mdbook-runnable use std::mem; @@ -63,11 +61,15 @@ fn main() { // They are of the form [starting_index..ending_index] // starting_index is the first position in the slice // ending_index is one more than the last position in the slice + // スライスは配列の一部を指すことができる。 + // [starting_index..ending_index] の形をとり、 + // starting_index はスライスの先頭の位置を表し、 + // ending_index はスライスの末尾の1つ先の位置を表す。 println!("borrow a section of the array as a slice"); analyze_slice(&ys[1 .. 4]); // Out of bound indexing causes compile error - // インデックスの範囲が配列のサイズを超えた場合パニックする + // インデックスが範囲外のときはコンパイルエラー println!("{}", xs[5]); } ``` diff --git a/src/primitives/tuples.md b/src/primitives/tuples.md index d24ee3b23..e4a347e96 100644 --- a/src/primitives/tuples.md +++ b/src/primitives/tuples.md @@ -49,9 +49,11 @@ fn main() { println!("tuple of tuples: {:?}", tuple_of_tuples); // But long Tuples cannot be printed + // しかし長すぎるタプルはプリントできない // let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); // println!("too long tuple: {:?}", too_long_tuple); // TODO ^ Uncomment the above 2 lines to see the compiler error + // TODO ^ 上記2行のコメントを外して、コンパイルエラーになることを確認 let pair = (1, true); println!("pair is {:?}", pair); diff --git a/src/variable_bindings/freeze.md b/src/variable_bindings/freeze.md index 228d2052d..6970eb321 100644 --- a/src/variable_bindings/freeze.md +++ b/src/variable_bindings/freeze.md @@ -2,9 +2,11 @@ # Freezing --> # 値のフリーズ - + +データを同じ名前のイミュータブルな変数に束縛することを、データを*フリーズ*するといいます。*フリーズされた*データは、イミュータブルな束縛がスコープ外になるまで変更できません。 ```rust,editable,ignore,mdbook-runnable fn main() { @@ -12,6 +14,7 @@ fn main() { { // Shadowing by immutable `_mutable_integer` + // イミュータブルな`_mutable_integer`でシャドーイングする let _mutable_integer = _mutable_integer; // Error! `_mutable_integer` is frozen in this scope From 17698c9593389cafd0b0f6da09652ceec633276d Mon Sep 17 00:00:00 2001 From: blueblade Date: Mon, 3 May 2021 12:31:52 +0900 Subject: [PATCH 02/10] fix to pass tests --- src/conversion/from_into.md | 2 +- src/variable_bindings/freeze.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/conversion/from_into.md b/src/conversion/from_into.md index 058e76a8a..28c523cc9 100644 --- a/src/conversion/from_into.md +++ b/src/conversion/from_into.md @@ -1,4 +1,3 @@ - @@ -30,6 +29,7 @@ For example we can easily convert a `str` into a `String` let my_str = "hello"; let my_string = String::from(my_str); ``` + diff --git a/src/variable_bindings/freeze.md b/src/variable_bindings/freeze.md index 6970eb321..c2a148604 100644 --- a/src/variable_bindings/freeze.md +++ b/src/variable_bindings/freeze.md @@ -2,6 +2,7 @@ # Freezing --> # 値のフリーズ + -# 値のフリーズ +# 値の凍結 -データを同じ名前のイミュータブルな変数に束縛することを、データを*フリーズ*するといいます。*フリーズされた*データは、イミュータブルな束縛がスコープ外になるまで変更できません。 +データを同じ名前のイミュータブルな変数に束縛しなおすと、データは*凍結*されます。*凍結*したデータは、イミュータブルな束縛がスコープ外になるまで変更できません。 ```rust,editable,ignore,mdbook-runnable fn main() { @@ -19,7 +19,7 @@ fn main() { let _mutable_integer = _mutable_integer; // Error! `_mutable_integer` is frozen in this scope - // エラー! `_mutable_integer`はこのスコープではフリーズしている。 + // エラー! `_mutable_integer`はこのスコープでは凍結している。 _mutable_integer = 50; // FIXME ^ Comment out this line // FIXME ^ この行をコメントアウトしましょう。 @@ -29,7 +29,7 @@ fn main() { } // Ok! `_mutable_integer` is not frozen in this scope - // OK! `_mutable_integer`はこのスコープではフリーズしていない。 + // OK! `_mutable_integer`はこのスコープでは凍結していない。 _mutable_integer = 3; } ``` From 3c2ebe74c808ac95c472aa410f5b0e2ec31a8eda Mon Sep 17 00:00:00 2001 From: blueblade Date: Mon, 3 May 2021 12:49:19 +0900 Subject: [PATCH 04/10] fix translation in summary --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 889d4f495..2a30092c5 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -58,7 +58,7 @@ - [ミュータビリティ](variable_bindings/mut.md) - [スコープとシャドーイング](variable_bindings/scope.md) - [宣言](variable_bindings/declare.md) - - [値のフリーズ](variable_bindings/freeze.md) + - [値の凍結](variable_bindings/freeze.md) -- [変数バインディング](variable_bindings.md) +- [変数束縛](variable_bindings.md) - [ミュータビリティ](variable_bindings/mut.md) - [スコープとシャドーイング](variable_bindings/scope.md) - [宣言](variable_bindings/declare.md) @@ -81,8 +81,8 @@ --> - [型変換](conversion.md) - [`From`および`Into`](conversion/from_into.md) - - [`TryFrom` and `TryInto`](conversion/try_from_try_into.md) - - [To and from `String`s](conversion/string.md) + - [`TryFrom`および`TryInto`](conversion/try_from_try_into.md) + - [`String`との型変換](conversion/string.md) -Rustは型の変換を[トレイト][traits]を用いて行います。一般的な型変換には[`From`]および[`Into`]トレイトを使用します。しかし、よくあるケースにおいて、特に`String`との相互の型変換では、特殊なトレイトが使用されます。 +Rustは型の変換を[トレイト][traits]を用いて行います。ジェネリックな型変換には[`From`]および[`Into`]トレイトを使用します。しかし、よくあるケースにおいて、特に`String`との相互の型変換では、特殊なトレイトが使用されます。 [traits]: trait.md [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html diff --git a/src/conversion/from_into.md b/src/conversion/from_into.md index 28c523cc9..e031cad15 100644 --- a/src/conversion/from_into.md +++ b/src/conversion/from_into.md @@ -18,7 +18,7 @@ type, hence providing a very simple mechanism for converting between several types. There are numerous implementations of this trait within the standard library for conversion of primitive and common types. --> -[`From`]トレイトは、ある型に対し、別の型からその型を作る方法を定義できるようにするものです。そのため、複数の型の間で型変換を行うための非常にシンプルな仕組みを提供しています。標準ライブラリでは、基本型やよく使われる型に対して、このトレイトが多数実装されています。 +[`From`]トレイトは、ある型に対し、別の型からその型を作る方法を定義できるようにするものです。そのため、複数の型の間で型変換を行うための非常にシンプルな仕組みを提供しています。標準ライブラリでは、基本データ型やよく使われる型に対して、このトレイトが多数実装されています。 +# Stringとの型変換 + +## Stringへの型変換 + +任意の型を`String`に変換するのは簡単で、その型に[`ToString`]トレイトを実装するだけです。これを直接実装するよりも、[`fmt::Display`][Display]トレイトを実装するのがよいでしょう。そうすることで自動的に[`ToString`]が提供されるだけでなく、[`print!`][print]の章で説明したように、その型を表示できるようにもなります。 ```rust,editable use std::fmt; @@ -26,17 +35,26 @@ fn main() { } ``` + +## Stringの解析 + +文字列からの型変換において、数値への型変換はよく行われるものの一つです。これを行うイディオムは[`parse`]関数を使用することですが、このときに型を推論できるようにするか、もしくは turbofish構文を使用して型を指定するかのいずれかを行います。以下の例では、どちらの方法も紹介しています。 + +`parse`関数は、指定された型に`FromStr`トレイトが実装されていれば、文字列をその型に変換します。このトレイトは標準ライブラリの多くの型に対して実装されています。ユーザー定義の型でこの機能を利用するには、その型に対して`FromStr`トレイトを実装するだけです。 ```rust,editable fn main() { diff --git a/src/conversion/try_from_try_into.md b/src/conversion/try_from_try_into.md index ac62ad154..66ab59627 100644 --- a/src/conversion/try_from_try_into.md +++ b/src/conversion/try_from_try_into.md @@ -1,9 +1,16 @@ + +# `TryFrom`および`TryInto` + +[`From`および`Into`][from-into]と同様に、[`TryFrom`]および[`TryInto`]も型変換を行うジェネリックなトレイトです。`From`/`Into`と異なり、`TryFrom`/`TryInto`トレイトは失敗する可能性のある型変換に用いられるので、[`Result`]を返します。 + [from-into]: from_into.html [`TryFrom`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html diff --git a/src/variable_bindings.md b/src/variable_bindings.md index 5c9120972..1bd3a5f05 100644 --- a/src/variable_bindings.md +++ b/src/variable_bindings.md @@ -1,7 +1,7 @@ -# 変数バインディング +# 変数束縛 -Rustは静的(`static`)な型付けゆえに型安全です。変数は宣言時に型を明示できます。とはいえたいていの場合は、コンパイラは変数の型をコンテキストから推測することができますので、常に苦労して明示する必要があるわけではありません。 +Rustは静的(`static`)な型付けゆえに型安全です。変数束縛は宣言時に型を指定できます。とはいえたいていの場合は、コンパイラは変数の型をコンテキストから推測することができますので、型指定の負担を大幅に軽減できます。 -値は(リテラルに似て)`let`を用いて変数に束縛されることができる。 +値(リテラルなど)は`let`を用いて変数に束縛することができます。 ```rust,editable fn main() { From d447fc5c195505b059af9cf1adc5de4ebce3e3a9 Mon Sep 17 00:00:00 2001 From: blueblade Date: Mon, 3 May 2021 16:56:07 +0900 Subject: [PATCH 06/10] translate chapter 8.2.2 --- src/SUMMARY.md | 2 +- src/flow_control/loop/return.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index f203b9c05..f599033ff 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -112,7 +112,7 @@ - [if/else](flow_control/if_else.md) - [loop](flow_control/loop.md) - [ネストとラベル](flow_control/loop/nested.md) - - [Returning from loops](flow_control/loop/return.md) + - [loopが返す値](flow_control/loop/return.md) - [while](flow_control/while.md) - [for と range](flow_control/for.md) - [match](flow_control/match.md) diff --git a/src/flow_control/loop/return.md b/src/flow_control/loop/return.md index 8511b1e86..57275e5b8 100644 --- a/src/flow_control/loop/return.md +++ b/src/flow_control/loop/return.md @@ -1,9 +1,15 @@ + +# loopが返す値 + +`loop`の用途のひとつに「成功するまである処理を再試行する」ことがあります。もしその処理が値を返すならば、それをコードの他の部分に渡す必要があるでしょう。`break`の後に値を置くと、それが`loop`式の値として返されます。 ```rust,editable fn main() { From e2cf452194f674b83d31d8fa190ebc133c078080 Mon Sep 17 00:00:00 2001 From: blueblade Date: Mon, 3 May 2021 18:14:26 +0900 Subject: [PATCH 07/10] translate untranslated lines in 8.5 --- src/flow_control/for.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/flow_control/for.md b/src/flow_control/for.md index f14d57567..fae4e718c 100644 --- a/src/flow_control/for.md +++ b/src/flow_control/for.md @@ -1,4 +1,7 @@ + +# forループ +上記の代わりに`a..=b`を用いると、両端の値を含む範囲を指定できます。上記の例は次のように書けます。 ```rust,editable fn main() { // `n` will take the values: 1, 2, ..., 100 in each iteration + // `n`は1, 2, ...., 100のそれぞれの値を取ります。 for n in 1..=100 { if n % 15 == 0 { println!("fizzbuzz"); @@ -56,19 +63,31 @@ fn main() { } ``` + +## forとイテレータ + +`for in`構文は`Iterator`とさまざまな方法でやり取りできます。[Iterator][iter]トレイトの章で説明したように、デフォルトでは`for`ループにおいて`into_iter`関数がコレクションに対して適用されます。しかし、コレクションをイテレータに変換する方法はこれだけではありません。 + +`into_iter`、`iter`、`iter_mut`はいずれもコレクションのイテレータへの変換を行いますが、データの「見せ方」の違いにより、そのやり方はそれぞれ異なります。 + +* `iter` - この関数は、各周回においてコレクションの要素を借用します。よってコレクションには手を加えないので、ループの実行後もコレクションを再利用できます。 ```rust, editable fn main() { @@ -83,9 +102,12 @@ fn main() { } ``` + +* `into_iter` - この関数はコレクションからデータを取り出すので、各周回において要素のデータそのものが提供されます。データを取り出してしまうと、データはループ内に「移動」してしまうので、ループ実行後にコレクションを再利用することはできません。 ```rust, editable fn main() { @@ -100,8 +122,11 @@ fn main() { } ``` + +* `iter_mut` - この関数はコレクションの各要素をミュータブル(変更可能)で借用するので、コレクションの要素をその場で変更できます。 ```rust, editable fn main() { @@ -118,9 +143,12 @@ fn main() { } ``` + +上記に示した3つのコードにおいて、`match`の選択肢の型の違いに注意してください。ここがそれぞれの方法の違いを生む鍵になっています。型が異なれば、当然ながらそれに対して行える処理も変わります。 `match`は値をさまざまなやり方でデストラクトすることができます。 + +* [タプルのデストラクト][tuple] +* [列挙型のデストラクト][enum] +* [ポインタのデストラクト][refs] +* [構造体のデストラクト][struct] [enum]: destructuring/destructure_enum.md From 16c4f345fe8910cd11959315a47c9fde7bd91ea6 Mon Sep 17 00:00:00 2001 From: blueblade Date: Mon, 3 May 2021 19:16:45 +0900 Subject: [PATCH 10/10] translate untranslated lines in chapter 9.2 --- src/fn/closures.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/fn/closures.md b/src/fn/closures.md index 98597f154..8cd6a36cf 100644 --- a/src/fn/closures.md +++ b/src/fn/closures.md @@ -3,22 +3,34 @@ --> # クロージャ + +Rustにおけるクロージャは、ラムダ式またはラムダとも呼ばれますが、その外側の環境を捕捉した関数のことです。例えば、次のコードは変数xを捕捉したクロージャです。 ```Rust |val| val + x ``` + +クロージャの構文や機能は、その場限りの用途で何かを作るのに便利です。クロージャの呼び出しは関数の呼び出しと全く同じです。しかし、入力の型と戻り値の型は推論させることができますが、入力変数の名前は必ず指定しなくてはなりません。 + +クロージャの他の特徴を以下に示します。 +* 入力変数を囲むのに、`()`の代わりに`||`を用います。 +* 本体が単一の式の場合は、本体の区切り文字(`{}`)を省略できます。(それ以外の場合は必須です) +* 外側の環境にある変数を捕捉することができます。 ```rust,editable fn main() { @@ -45,6 +57,8 @@ fn main() { // A closure taking no arguments which returns an `i32`. // The return type is inferred. + // 引数なしで`i32`を返すクロージャ。 + // 戻り値の型は推論された。 let one = || 1; println!("closure returning one: {}", one()); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy