Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions 1.6/ja/book/borrow-and-asref.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
% Borrow and AsRef
% BorrowとAsRef
<!-- % Borrow and AsRef -->

The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but
different. Here’s a quick refresher on what these two traits mean.
<!-- 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 -->
# 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.
<!-- 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`:
<!-- For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`: -->
例えば、 [`HashMap`][hashmap] には `Borrow` を使った [`get` メソッド][get] があります。

```rust,ignore
fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
Expand All @@ -22,17 +27,19 @@ fn get<Q: ?Sized>(&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:
<!-- 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<K, V, S = RandomState> {
```

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<Q>`. That way, we can make a `HashMap` which uses `String` keys,
but use `&str`s when we’re searching:
<!-- 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<Q>`. That way, we can make a `HashMap` which uses `String` keys, -->
<!-- but use `&str`s when we’re searching: -->
`K` パラメータは `HashMap` の「キー」を表す型です。ここで再び `get()` のシグネチャを見ると、キーが `Borrow<Q>` を実装しているときに `get()` を使えることが分かります。そのため、以下のように `String` をキーとした `HashMap` を検索するときに `&str` を使うことができます。

```rust
use std::collections::HashMap;
Expand All @@ -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<str> for String`.
<!-- This is because the standard library has `impl Borrow<str> for String`. -->
これは標準ライブラリが `impl Borrow<str> 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:
<!-- 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;
Expand All @@ -65,12 +74,15 @@ foo(&i);
foo(&mut i);
```

This will print out `a is borrowed: 5` twice.
<!-- This will print out `a is borrowed: 5` twice. -->
上のコードは `a is borrowed: 5` を二度出力します。

<!-- # AsRef -->
# AsRef

The `AsRef` trait is a conversion trait. It’s used for converting some value to
a reference in generic code. Like this:
<!-- 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();
Expand All @@ -80,14 +92,18 @@ fn foo<T: AsRef<str>>(s: T) {
}
```

# Which should I use?
<!-- # 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.
<!-- 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.
<!-- 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.
<!-- Choose `AsRef` when you want to convert something to a reference directly, and -->
<!-- you’re writing generic code. -->
ジェネリックなコードで値を参照に直接変換したい場合は `AsRef` を使ってください。
2 changes: 2 additions & 0 deletions TranslationTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
| interpolate | インターポーレートする
| interpolation | インターポーレーション
| Intrinsics | Intrinsic
| key | キー
| keyword | キーワード
| Lang Items | Lang Item
| leak | リーク
Expand Down Expand Up @@ -116,6 +117,7 @@
| owner | 所有者
| ownership | 所有権
| panic | パニック
| parameter | パラメータ
| parametric polymorphism | パラメトリック多相
| parse | パース、パースする
| patch | パッチ
Expand Down