From 78b93559d126b4bb2406ed1c6ee3cd5e7df2fd01 Mon Sep 17 00:00:00 2001 From: "Tatsuya Kawano (Circle CI)" Date: Fri, 24 Jun 2016 08:48:23 +0800 Subject: [PATCH] Updating Vectors to 1.9 (WIP) --- 1.9/ja/book/vectors.md | 101 ++++++++++++++++--- diff-1.6.0..1.9.0/src/doc/book/vectors.md | 115 ---------------------- 2 files changed, 88 insertions(+), 128 deletions(-) delete mode 100644 diff-1.6.0..1.9.0/src/doc/book/vectors.md diff --git a/1.9/ja/book/vectors.md b/1.9/ja/book/vectors.md index aa2f3a15..65a8d831 100644 --- a/1.9/ja/book/vectors.md +++ b/1.9/ja/book/vectors.md @@ -6,9 +6,9 @@ -「ベクタ」は動的な、または「拡張可能な」配列です、標準ライブラリ上で [`Vec`][vec] として提供されています。 -`T` はどんなタイプのベクタをも作成することが可能なことを意味しています。(詳細は[ジェネリクス][generic]を御覧ください) -ベクタはデータを常にヒープ上にアロケーションします。 +「ベクタ」は動的な、または「拡張可能な」配列で、標準ライブラリ上で [`Vec`][vec] として提供されています。 +`T` はどんな型のベクタでも作成できることを意味します。(詳細は[ジェネリクス][generic]をご覧ください) +ベクタはデータを常にヒープ上に割り当てます。 ベクタは以下のように `vec!` マクロを用いて作成できます: ```rust @@ -16,19 +16,26 @@ let v = vec![1, 2, 3, 4, 5]; // v: Vec ``` - - -(以前使った`println!` マクロと異なり、`vec!` マクロで 角括弧 `[]` を利用しました。) -Rustではどちらの括弧もどちらのシチュエーションでも利用可能であり、解りやすさのためです。 + + +(以前使った`println!` マクロと異なり、`vec!` マクロでは角括弧 `[]` を利用しました。 +Rustでは状況に応じてどちらの括弧も利用でき、解りやすさのためです) -`vec!` には初期値の繰り返しを表現するための形式があります: +`vec!` には初期値の繰り返しを表現するための別の書き方があります: ```rust # // let v = vec![0; 10]; // ten zeroes let v = vec![0; 10]; // 0が10個 ``` + + + + + +**TODO**: このパラグラフを翻訳する。 + ## 要素へのアクセス @@ -62,11 +69,11 @@ v[j]; ``` -`usize` 型でないインデックスを用いた場合、以下の様なエラーが発生します: +`usize` 型でないインデックスを用いた場合、以下のようなエラーが発生します: ```text -error: the trait `core::ops::Index` is not implemented for the type -`collections::vec::Vec<_>` [E0277] +error: the trait bound `collections::vec::Vec<_> : core::ops::Index` +is not satisfied [E0277] v[j]; ^~~~ note: the type `collections::vec::Vec<_>` cannot be indexed by `i32` @@ -75,12 +82,44 @@ error: aborting due to previous error -エラーメッセージ中には多くの点が含まれていますが、一番大切な部分は `i32` をインデックスとして用いることはできないという点です。 +エラーメッセージには多くの点が含まれていますが、一番大切なのは `i32` をインデックスとして用いられない点です。 + +## Out-of-bounds Access + +**TODO**: この小節を翻訳する。 + + + +```ignore +let v = vec![1, 2, 3]; +println!("Item 7 is {}", v[7]); +``` + + + +```text +thread '
' panicked at 'index out of bounds: the len is 3 but the index is 7' +``` + + + + + +```rust +let v = vec![1, 2, 3]; +match v.get(7) { + Some(x) => println!("Item 7 is {}", x), + None => println!("Sorry, this vector is too short.") +} +``` ## イテレーティング +**TODO**: イテレーティングをイテレーションに変えるか検討する。 + +**TODO**: この訳を修正する。("Once you have a vector", "its elements", "three versions" などが抜けている) ベクタである値に対して `for` を用いて以下の様な3つの方法でイテレートすることができます: ```rust @@ -99,9 +138,45 @@ for i in v { } ``` + + + +**TODO**: Noteを翻訳する。 + +```rust,ignore +let v = vec![1, 2, 3, 4, 5]; + +for i in v { + println!("Take ownership of the vector and its element {}", i); +} + +for i in v { + println!("Take ownership of the vector and its element {}", i); +} +``` + + +**TODO**: この文を翻訳する。 + +```rust +let v = vec![1, 2, 3, 4, 5]; + +for i in &v { + println!("This is a reference to {}", i); +} + +for i in &v { + println!("This is a reference to {}", i); +} +``` + -ベクタにはもっと多くの便利なメソッドが定義されています。それらのメソッドについては [APIドキュメント][vec] で確認することができます。 +ベクタにはさらに多くの便利なメソッドが定義されています。それらのメソッドについては [APIドキュメント][vec] で確認できます。 [vec]: ../std/vec/index.html +[box]: ../std/boxed/index.html [generic]: generics.html +[panic]: concurrency.html#panics +[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get +[get_mut]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get_mut diff --git a/diff-1.6.0..1.9.0/src/doc/book/vectors.md b/diff-1.6.0..1.9.0/src/doc/book/vectors.md deleted file mode 100644 index 76e3c19f..00000000 --- a/diff-1.6.0..1.9.0/src/doc/book/vectors.md +++ /dev/null @@ -1,115 +0,0 @@ ---- a/src/doc/book/vectors.md -+++ b/src/doc/book/vectors.md -@@ -11,8 +11,8 @@ let v = vec![1, 2, 3, 4, 5]; // v: Vec - ``` - - (Notice that unlike the `println!` macro we’ve used in the past, we use square --brackets `[]` with `vec!` macro. Rust allows you to use either in either situation, --this is just convention.) -+brackets `[]` with `vec!` macro. Rust allows you to use either in either -+situation, this is just convention.) - - There’s an alternate form of `vec!` for repeating an initial value: - -@@ -20,6 +20,12 @@ There’s an alternate form of `vec!` for repeating an initial value: - let v = vec![0; 10]; // ten zeroes - ``` - -+Vectors store their contents as contiguous arrays of `T` on the heap. This means -+that they must be able to know the size of `T` at compile time (that is, how -+many bytes are needed to store a `T`?). The size of some things can't be known -+at compile time. For these you'll have to store a pointer to that thing: -+thankfully, the [`Box`][box] type works perfectly for this. -+ - ## Accessing elements - - To get the value at a particular index in the vector, we use `[]`s: -@@ -50,8 +56,8 @@ v[j]; - Indexing with a non-`usize` type gives an error that looks like this: - - ```text --error: the trait `core::ops::Index` is not implemented for the type --`collections::vec::Vec<_>` [E0277] -+error: the trait bound `collections::vec::Vec<_> : core::ops::Index` -+is not satisfied [E0277] - v[j]; - ^~~~ - note: the type `collections::vec::Vec<_>` cannot be indexed by `i32` -@@ -61,6 +67,33 @@ error: aborting due to previous error - There’s a lot of punctuation in that message, but the core of it makes sense: - you cannot index with an `i32`. - -+## Out-of-bounds Access -+ -+If you try to access an index that doesn’t exist: -+ -+```ignore -+let v = vec![1, 2, 3]; -+println!("Item 7 is {}", v[7]); -+``` -+ -+then the current thread will [panic] with a message like this: -+ -+```text -+thread '
' panicked at 'index out of bounds: the len is 3 but the index is 7' -+``` -+ -+If you want to handle out-of-bounds errors without panicking, you can use -+methods like [`get`][get] or [`get_mut`][get_mut] that return `None` when -+given an invalid index: -+ -+```rust -+let v = vec![1, 2, 3]; -+match v.get(7) { -+ Some(x) => println!("Item 7 is {}", x), -+ None => println!("Sorry, this vector is too short.") -+} -+``` -+ - ## Iterating - - Once you have a vector, you can iterate through its elements with `for`. There -@@ -82,8 +115,42 @@ for i in v { - } - ``` - -+Note: You cannot use the vector again once you have iterated by taking ownership of the vector. -+You can iterate the vector multiple times by taking a reference to the vector whilst iterating. -+For example, the following code does not compile. -+ -+```rust,ignore -+let v = vec![1, 2, 3, 4, 5]; -+ -+for i in v { -+ println!("Take ownership of the vector and its element {}", i); -+} -+ -+for i in v { -+ println!("Take ownership of the vector and its element {}", i); -+} -+``` -+ -+Whereas the following works perfectly, -+ -+```rust -+let v = vec![1, 2, 3, 4, 5]; -+ -+for i in &v { -+ println!("This is a reference to {}", i); -+} -+ -+for i in &v { -+ println!("This is a reference to {}", i); -+} -+``` -+ - Vectors have many more useful methods, which you can read about in [their - API documentation][vec]. - - [vec]: ../std/vec/index.html -+[box]: ../std/boxed/index.html - [generic]: generics.html -+[panic]: concurrency.html#panics -+[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get -+[get_mut]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get_mut -diff --git a/src/doc/complement-design-faq.md b/src/doc/complement-design-faq.md