From 33d609efff690ae8d6821ab1714fea82ad30b955 Mon Sep 17 00:00:00 2001 From: dalance Date: Sun, 21 Feb 2016 21:44:58 +0900 Subject: [PATCH] traslate borrow-and-asref --- 1.6/ja/book/borrow-and-asref.md | 74 ++++++++++++++++++++------------- TranslationTable.md | 2 + 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/1.6/ja/book/borrow-and-asref.md b/1.6/ja/book/borrow-and-asref.md index 1cfeb262..5f473f08 100644 --- a/1.6/ja/book/borrow-and-asref.md +++ b/1.6/ja/book/borrow-and-asref.md @@ -1,17 +1,22 @@ -% Borrow and AsRef +% BorrowとAsRef + -The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but -different. Here’s a quick refresher on what these two traits mean. + + +[`Borrow`][borrow] トレイトと [`AsRef`][asref] トレイトはとてもよく似ていますが違うものです。ここでは2つのトレイトの意味を簡単に説明します。 [borrow]: ../std/borrow/trait.Borrow.html [asref]: ../std/convert/trait.AsRef.html + # Borrow -The `Borrow` trait is used when you’re writing a datastructure, and you want to -use either an owned or borrowed type as synonymous for some purpose. + + +`Borrow` トレイトはデータ構造を書いていて、所有型と借用型を同等に扱いたいときに使います。 -For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`: + +例えば、 [`HashMap`][hashmap] には `Borrow` を使った [`get` メソッド][get] があります。 ```rust,ignore fn get(&self, k: &Q) -> Option<&V> @@ -22,17 +27,19 @@ fn get(&self, k: &Q) -> Option<&V> [hashmap]: ../std/collections/struct.HashMap.html [get]: ../std/collections/struct.HashMap.html#method.get -This signature is pretty complicated. The `K` parameter is what we’re interested -in here. It refers to a parameter of the `HashMap` itself: + + +このシグネチャは少し複雑です。`K` パラメータに注目してください。これは以下のように `HashMap` 自身のパラメータになっています。 ```rust,ignore struct HashMap { ``` -The `K` parameter is the type of _key_ the `HashMap` uses. So, looking at -the signature of `get()` again, we can use `get()` when the key implements -`Borrow`. That way, we can make a `HashMap` which uses `String` keys, -but use `&str`s when we’re searching: + + + + +`K` パラメータは `HashMap` の「キー」を表す型です。ここで再び `get()` のシグネチャを見ると、キーが `Borrow` を実装しているときに `get()` を使えることが分かります。そのため、以下のように `String` をキーとした `HashMap` を検索するときに `&str` を使うことができます。 ```rust use std::collections::HashMap; @@ -43,13 +50,15 @@ map.insert("Foo".to_string(), 42); assert_eq!(map.get("Foo"), Some(&42)); ``` -This is because the standard library has `impl Borrow for String`. + +これは標準ライブラリが `impl Borrow for String` を提供しているためです。 -For most types, when you want to take an owned or borrowed type, a `&T` is -enough. But one area where `Borrow` is effective is when there’s more than one -kind of borrowed value. This is especially true of references and slices: you -can have both an `&T` or a `&mut T`. If we wanted to accept both of these types, -`Borrow` is up for it: + + + + + +所有型か借用型のどちらかを取りたい場合、たいていは `&T` で十分ですが、借用された値が複数種類ある場合 `Borrow` が役に立ちます。特に参照とスライスは `&T` と `&mut T` のいずれも取りうるため、そのどちらも受け入れたい場合は `Borrow` がよいでしょう。 ```rust use std::borrow::Borrow; @@ -65,12 +74,15 @@ foo(&i); foo(&mut i); ``` -This will print out `a is borrowed: 5` twice. + +上のコードは `a is borrowed: 5` を二度出力します。 + # AsRef -The `AsRef` trait is a conversion trait. It’s used for converting some value to -a reference in generic code. Like this: + + +`AsRef` トレイトは変換用のトレイトです。ジェネリックなコードにおいて、値を参照に変換したい場合に使います。 ```rust let s = "Hello".to_string(); @@ -80,14 +92,18 @@ fn foo>(s: T) { } ``` -# Which should I use? + +# どちらを使うべきか -We can see how they’re kind of the same: they both deal with owned and borrowed -versions of some type. However, they’re a bit different. + + +ここまでで見てきた通り、2つのトレイトは、どちらもある型の所有型バージョンと借用型バージョンの両方を扱う、という意味で同じような種類のものですが、少し違います。 -Choose `Borrow` when you want to abstract over different kinds of borrowing, or -when you’re building a datastructure that treats owned and borrowed values in -equivalent ways, such as hashing and comparison. + + + +いくつかの異なる種類の借用を抽象化したい場合や、ハッシュ化や比較のために所有型と借用型を同等に扱いたいデータ構造を作る場合は `Borrow` を使ってください。 -Choose `AsRef` when you want to convert something to a reference directly, and -you’re writing generic code. + + +ジェネリックなコードで値を参照に直接変換したい場合は `AsRef` を使ってください。 diff --git a/TranslationTable.md b/TranslationTable.md index d02f1217..acb34944 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -88,6 +88,7 @@ | interpolate | インターポーレートする | interpolation | インターポーレーション | Intrinsics | Intrinsic +| key | キー | keyword | キーワード | Lang Items | Lang Item | leak | リーク @@ -116,6 +117,7 @@ | owner | 所有者 | ownership | 所有権 | panic | パニック +| parameter | パラメータ | parametric polymorphism | パラメトリック多相 | parse | パース、パースする | patch | パッチ