1- % Borrow and AsRef
1+ % BorrowとAsRef
2+ <!-- % Borrow and AsRef -->
23
3- The [ ` Borrow ` ] [ borrow ] and [ ` AsRef ` ] [ asref ] traits are very similar, but
4- different. Here’s a quick refresher on what these two traits mean.
4+ <!-- The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but -->
5+ <!-- different. Here’s a quick refresher on what these two traits mean. -->
6+ [ ` Borrow ` ] [ borrow ] トレイトと [ ` AsRef ` ] [ asref ] トレイトはとてもよく似ていますが違うものです。ここでは2つのトレイトの意味を簡単に説明します。
57
68[ borrow ] : ../std/borrow/trait.Borrow.html
79[ asref ] : ../std/convert/trait.AsRef.html
810
11+ <!-- # Borrow -->
912# Borrow
1013
11- The ` Borrow ` trait is used when you’re writing a datastructure, and you want to
12- use either an owned or borrowed type as synonymous for some purpose.
14+ <!-- The `Borrow` trait is used when you’re writing a datastructure, and you want to -->
15+ <!-- use either an owned or borrowed type as synonymous for some purpose. -->
16+ ` Borrow ` トレイトはデータ構造を書いていて、所有型と借用型を同等に扱いたいときに使います。
1317
14- For example, [ ` HashMap ` ] [ hashmap ] has a [ ` get ` method] [ get ] which uses ` Borrow ` :
18+ <!-- For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`: -->
19+ 例えば、 [ ` HashMap ` ] [ hashmap ] には ` Borrow ` を使った [ ` get ` メソッド] [ get ] があります。
1520
1621``` rust,ignore
1722fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
@@ -22,17 +27,19 @@ fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
2227[ hashmap ] : ../std/collections/struct.HashMap.html
2328[ get ] : ../std/collections/struct.HashMap.html#method.get
2429
25- This signature is pretty complicated. The ` K ` parameter is what we’re interested
26- in here. It refers to a parameter of the ` HashMap ` itself:
30+ <!-- This signature is pretty complicated. The `K` parameter is what we’re interested -->
31+ <!-- in here. It refers to a parameter of the `HashMap` itself: -->
32+ このシグネチャは少し複雑です。` K ` パラメータに注目してください。これは以下のように ` HashMap ` 自身のパラメータになっています。
2733
2834``` rust,ignore
2935struct HashMap<K, V, S = RandomState> {
3036```
3137
32- The ` K ` parameter is the type of _ key_ the ` HashMap ` uses. So, looking at
33- the signature of ` get() ` again, we can use ` get() ` when the key implements
34- ` Borrow<Q> ` . That way, we can make a ` HashMap ` which uses ` String ` keys,
35- but use ` &str ` s when we’re searching:
38+ <!-- The `K` parameter is the type of _key_ the `HashMap` uses. So, looking at -->
39+ <!-- the signature of `get()` again, we can use `get()` when the key implements -->
40+ <!-- `Borrow<Q>`. That way, we can make a `HashMap` which uses `String` keys, -->
41+ <!-- but use `&str`s when we’re searching: -->
42+ ` K ` パラメータは ` HashMap ` の「キー」を表す型です。ここで再び ` get() ` のシグネチャを見ると、キーが ` Borrow<Q> ` を実装しているときに ` get() ` を使えることが分かります。そのため、以下のように ` String ` をキーとした ` HashMap ` を検索するときに ` &str ` を使うことができます。
3643
3744``` rust
3845use std :: collections :: HashMap ;
@@ -43,13 +50,15 @@ map.insert("Foo".to_string(), 42);
4350assert_eq! (map . get (" Foo" ), Some (& 42 ));
4451```
4552
46- This is because the standard library has ` impl Borrow<str> for String ` .
53+ <!-- This is because the standard library has `impl Borrow<str> for String`. -->
54+ これは標準ライブラリが ` impl Borrow<str> for String ` を提供しているためです。
4755
48- For most types, when you want to take an owned or borrowed type, a ` &T ` is
49- enough. But one area where ` Borrow ` is effective is when there’s more than one
50- kind of borrowed value. This is especially true of references and slices: you
51- can have both an ` &T ` or a ` &mut T ` . If we wanted to accept both of these types,
52- ` Borrow ` is up for it:
56+ <!-- For most types, when you want to take an owned or borrowed type, a `&T` is -->
57+ <!-- enough. But one area where `Borrow` is effective is when there’s more than one -->
58+ <!-- kind of borrowed value. This is especially true of references and slices: you -->
59+ <!-- can have both an `&T` or a `&mut T`. If we wanted to accept both of these types, -->
60+ <!-- `Borrow` is up for it: -->
61+ 所有型か借用型のどちらかを取りたい場合、たいていは ` &T ` で十分ですが、借用された値が複数種類ある場合 ` Borrow ` が役に立ちます。特に参照とスライスは ` &T ` と ` &mut T ` のいずれも取りうるため、そのどちらも受け入れたい場合は ` Borrow ` がよいでしょう。
5362
5463``` rust
5564use std :: borrow :: Borrow ;
@@ -65,12 +74,15 @@ foo(&i);
6574foo (& mut i );
6675```
6776
68- This will print out ` a is borrowed: 5 ` twice.
77+ <!-- This will print out `a is borrowed: 5` twice. -->
78+ 上のコードは ` a is borrowed: 5 ` を二度出力します。
6979
80+ <!-- # AsRef -->
7081# AsRef
7182
72- The ` AsRef ` trait is a conversion trait. It’s used for converting some value to
73- a reference in generic code. Like this:
83+ <!-- The `AsRef` trait is a conversion trait. It’s used for converting some value to -->
84+ <!-- a reference in generic code. Like this: -->
85+ ` AsRef ` トレイトは変換用のトレイトです。ジェネリックなコードにおいて、値を参照に変換したい場合に使います。
7486
7587``` rust
7688let s = " Hello" . to_string ();
@@ -80,14 +92,18 @@ fn foo<T: AsRef<str>>(s: T) {
8092}
8193```
8294
83- # Which should I use?
95+ <!-- # Which should I use? -->
96+ # どちらを使うべきか
8497
85- We can see how they’re kind of the same: they both deal with owned and borrowed
86- versions of some type. However, they’re a bit different.
98+ <!-- We can see how they’re kind of the same: they both deal with owned and borrowed -->
99+ <!-- versions of some type. However, they’re a bit different. -->
100+ ここまでで見てきた通り、2つのトレイトは、どちらもある型の所有型バージョンと借用型バージョンの両方を扱う、という意味で同じような種類のものですが、少し違います。
87101
88- Choose ` Borrow ` when you want to abstract over different kinds of borrowing, or
89- when you’re building a datastructure that treats owned and borrowed values in
90- equivalent ways, such as hashing and comparison.
102+ <!-- Choose `Borrow` when you want to abstract over different kinds of borrowing, or -->
103+ <!-- when you’re building a datastructure that treats owned and borrowed values in -->
104+ <!-- equivalent ways, such as hashing and comparison. -->
105+ いくつかの異なる種類の借用を抽象化したい場合や、ハッシュ化や比較のために所有型と借用型を同等に扱いたいデータ構造を作る場合は ` Borrow ` を使ってください。
91106
92- Choose ` AsRef ` when you want to convert something to a reference directly, and
93- you’re writing generic code.
107+ <!-- Choose `AsRef` when you want to convert something to a reference directly, and -->
108+ <!-- you’re writing generic code. -->
109+ ジェネリックなコードで値を参照に直接変換したい場合は ` AsRef ` を使ってください。
0 commit comments