4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > Borrow and AsRef </ title >
7
+ < title > BorrowとAsRef </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
188
- < h1 class ="title "> Borrow and AsRef</ h1 >
189
- < p > The < a href ="../std/borrow/trait.Borrow.html "> < code > Borrow</ code > </ a > and < a href ="../std/convert/trait.AsRef.html "> < code > AsRef</ code > </ a > traits are very similar, but
190
- different. Here’s a quick refresher on what these two traits mean.</ p >
188
+ < h1 class ="title "> BorrowとAsRef</ h1 >
189
+ <!-- % Borrow and AsRef -->
190
+
191
+ <!-- The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but -->
192
+
193
+ <!-- different. Here’s a quick refresher on what these two traits mean. -->
194
+
195
+ < p > < a href ="../std/borrow/trait.Borrow.html "> < code > Borrow</ code > </ a > トレイトと < a href ="../std/convert/trait.AsRef.html "> < code > AsRef</ code > </ a > トレイトはとてもよく似ていますが違うものです。ここでは2つのトレイトの意味を簡単に説明します。</ p >
196
+
197
+ <!-- # Borrow -->
191
198
192
199
< h1 id ='borrow ' class ='section-header '> < a href ='#borrow '> Borrow</ a > </ h1 >
193
- < p > The < code > Borrow</ code > trait is used when you’re writing a datastructure, and you want to
194
- use either an owned or borrowed type as synonymous for some purpose.</ p >
200
+ <!-- The `Borrow` trait is used when you’re writing a datastructure, and you want to -->
195
201
196
- < p > For example, < a href ="../std/collections/struct.HashMap.html "> < code > HashMap</ code > </ a > has a < a href ="../std/collections/struct.HashMap.html#method.get "> < code > get</ code > method</ a > which uses < code > Borrow</ code > :</ p >
202
+ <!-- use either an owned or borrowed type as synonymous for some purpose. -->
203
+
204
+ < p > < code > Borrow</ code > トレイトはデータ構造を書いていて、所有型と借用型を同等に扱いたいときに使います。</ p >
205
+
206
+ <!-- For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`: -->
207
+
208
+ < p > 例えば、 < a href ="../std/collections/struct.HashMap.html "> < code > HashMap</ code > </ a > には < code > Borrow</ code > を使った < a href ="../std/collections/struct.HashMap.html#method.get "> < code > get</ code > メソッド</ a > があります。</ p >
197
209
< span class ='rusttest '> fn main() {
198
210
fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
199
211
where K: Borrow<Q>,
@@ -203,17 +215,25 @@ <h1 id='borrow' class='section-header'><a href='#borrow'>Borrow</a></h1>
203
215
< span class ='kw '> where</ span > < span class ='ident '> K</ span > : < span class ='ident '> Borrow</ span > < span class ='op '> <</ span > < span class ='ident '> Q</ span > < span class ='op '> ></ span > ,
204
216
< span class ='ident '> Q</ span > : < span class ='ident '> Hash</ span > < span class ='op '> +</ span > < span class ='ident '> Eq</ span > </ pre >
205
217
206
- < p > This signature is pretty complicated. The < code > K</ code > parameter is what we’re interested
207
- in here. It refers to a parameter of the < code > HashMap</ code > itself:</ p >
218
+ <!-- This signature is pretty complicated. The `K` parameter is what we’re interested -->
219
+
220
+ <!-- in here. It refers to a parameter of the `HashMap` itself: -->
221
+
222
+ < p > このシグネチャは少し複雑です。< code > K</ code > パラメータに注目してください。これは以下のように < code > HashMap</ code > 自身のパラメータになっています。</ p >
208
223
< span class ='rusttest '> fn main() {
209
224
struct HashMap<K, V, S = RandomState> {
210
225
}</ span > < pre class ='rust rust-example-rendered '>
211
226
< span class ='kw '> struct</ span > < span class ='ident '> HashMap</ span > < span class ='op '> <</ span > < span class ='ident '> K</ span > , < span class ='ident '> V</ span > , < span class ='ident '> S</ span > < span class ='op '> =</ span > < span class ='ident '> RandomState</ span > < span class ='op '> ></ span > {</ pre >
212
227
213
- < p > The < code > K</ code > parameter is the type of < em > key</ em > the < code > HashMap</ code > uses. So, looking at
214
- the signature of < code > get()</ code > again, we can use < code > get()</ code > when the key implements
215
- < code > Borrow<Q></ code > . That way, we can make a < code > HashMap</ code > which uses < code > String</ code > keys,
216
- but use < code > &str</ code > s when we’re searching:</ p >
228
+ <!-- The `K` parameter is the type of _key_ the `HashMap` uses. So, looking at -->
229
+
230
+ <!-- the signature of `get()` again, we can use `get()` when the key implements -->
231
+
232
+ <!-- `Borrow<Q>`. That way, we can make a `HashMap` which uses `String` keys, -->
233
+
234
+ <!-- but use `&str`s when we’re searching: -->
235
+
236
+ < p > < code > K</ code > パラメータは < code > HashMap</ code > の「キー」を表す型です。ここで再び < code > get()</ code > のシグネチャを見ると、キーが < code > Borrow<Q></ code > を実装しているときに < code > get()</ code > を使えることが分かります。そのため、以下のように < code > String</ code > をキーとした < code > HashMap</ code > を検索するときに < code > &str</ code > を使うことができます。</ p >
217
237
< span class ='rusttest '> fn main() {
218
238
use std::collections::HashMap;
219
239
@@ -229,13 +249,21 @@ <h1 id='borrow' class='section-header'><a href='#borrow'>Borrow</a></h1>
229
249
230
250
< span class ='macro '> assert_eq</ span > < span class ='macro '> !</ span > (< span class ='ident '> map</ span > .< span class ='ident '> get</ span > (< span class ='string '> "Foo"</ span > ), < span class ='prelude-val '> Some</ span > (< span class ='kw-2 '> &</ span > < span class ='number '> 42</ span > ));</ pre >
231
251
232
- < p > This is because the standard library has < code > impl Borrow<str> for String</ code > .</ p >
252
+ <!-- This is because the standard library has `impl Borrow<str> for String`. -->
253
+
254
+ < p > これは標準ライブラリが < code > impl Borrow<str> for String</ code > を提供しているためです。</ p >
255
+
256
+ <!-- For most types, when you want to take an owned or borrowed type, a `&T` is -->
257
+
258
+ <!-- enough. But one area where `Borrow` is effective is when there’s more than one -->
233
259
234
- < p > For most types, when you want to take an owned or borrowed type, a < code > &T</ code > is
235
- enough. But one area where < code > Borrow</ code > is effective is when there’s more than one
236
- kind of borrowed value. This is especially true of references and slices: you
237
- can have both an < code > &T</ code > or a < code > &mut T</ code > . If we wanted to accept both of these types,
238
- < code > Borrow</ code > is up for it:</ p >
260
+ <!-- kind of borrowed value. This is especially true of references and slices: you -->
261
+
262
+ <!-- can have both an `&T` or a `&mut T`. If we wanted to accept both of these types, -->
263
+
264
+ <!-- `Borrow` is up for it: -->
265
+
266
+ < p > 所有型か借用型のどちらかを取りたい場合、たいていは < code > &T</ code > で十分ですが、借用された値が複数種類ある場合 < code > Borrow</ code > が役に立ちます。特に参照とスライスは < code > &T</ code > と < code > &mut T</ code > のいずれも取りうるため、そのどちらも受け入れたい場合は < code > Borrow</ code > がよいでしょう。</ p >
239
267
< span class ='rusttest '> fn main() {
240
268
use std::borrow::Borrow;
241
269
use std::fmt::Display;
@@ -261,11 +289,18 @@ <h1 id='borrow' class='section-header'><a href='#borrow'>Borrow</a></h1>
261
289
< span class ='ident '> foo</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> i</ span > );
262
290
< span class ='ident '> foo</ span > (< span class ='kw-2 '> &</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> i</ span > );</ pre >
263
291
264
- < p > This will print out < code > a is borrowed: 5</ code > twice.</ p >
292
+ <!-- This will print out `a is borrowed: 5` twice. -->
293
+
294
+ < p > 上のコードは < code > a is borrowed: 5</ code > を二度出力します。</ p >
295
+
296
+ <!-- # AsRef -->
265
297
266
298
< h1 id ='asref ' class ='section-header '> < a href ='#asref '> AsRef</ a > </ h1 >
267
- < p > The < code > AsRef</ code > trait is a conversion trait. It’s used for converting some value to
268
- a reference in generic code. Like this:</ p >
299
+ <!-- The `AsRef` trait is a conversion trait. It’s used for converting some value to -->
300
+
301
+ <!-- a reference in generic code. Like this: -->
302
+
303
+ < p > < code > AsRef</ code > トレイトは変換用のトレイトです。ジェネリックなコードにおいて、値を参照に変換したい場合に使います。</ p >
269
304
< span class ='rusttest '> fn main() {
270
305
let s = "Hello".to_string();
271
306
@@ -279,16 +314,28 @@ <h1 id='asref' class='section-header'><a href='#asref'>AsRef</a></h1>
279
314
< span class ='kw '> let</ span > < span class ='ident '> slice</ span > < span class ='op '> =</ span > < span class ='ident '> s</ span > .< span class ='ident '> as_ref</ span > ();
280
315
}</ pre >
281
316
282
- < h1 id ='which-should-i-use ' class ='section-header '> < a href ='#which-should-i-use '> Which should I use?</ a > </ h1 >
283
- < p > We can see how they’re kind of the same: they both deal with owned and borrowed
284
- versions of some type. However, they’re a bit different.</ p >
317
+ <!-- # Which should I use? -->
318
+
319
+ < h1 id ='どちらを使うべきか ' class ='section-header '> < a href ='#どちらを使うべきか '> どちらを使うべきか</ a > </ h1 >
320
+ <!-- We can see how they’re kind of the same: they both deal with owned and borrowed -->
321
+
322
+ <!-- versions of some type. However, they’re a bit different. -->
323
+
324
+ < p > ここまでで見てきた通り、2つのトレイトは、どちらもある型の所有型バージョンと借用型バージョンの両方を扱う、という意味で同じような種類のものですが、少し違います。</ p >
325
+
326
+ <!-- Choose `Borrow` when you want to abstract over different kinds of borrowing, or -->
327
+
328
+ <!-- when you’re building a datastructure that treats owned and borrowed values in -->
329
+
330
+ <!-- equivalent ways, such as hashing and comparison. -->
331
+
332
+ < p > いくつかの異なる種類の借用を抽象化したい場合や、ハッシュ化や比較のために所有型と借用型を同等に扱いたいデータ構造を作る場合は < code > Borrow</ code > を使ってください。</ p >
333
+
334
+ <!-- Choose `AsRef` when you want to convert something to a reference directly, and -->
285
335
286
- < p > Choose < code > Borrow</ code > when you want to abstract over different kinds of borrowing, or
287
- when you’re building a datastructure that treats owned and borrowed values in
288
- equivalent ways, such as hashing and comparison.</ p >
336
+ <!-- you’re writing generic code. -->
289
337
290
- < p > Choose < code > AsRef</ code > when you want to convert something to a reference directly, and
291
- you’re writing generic code.</ p >
338
+ < p > ジェネリックなコードで値を参照に直接変換したい場合は < code > AsRef</ code > を使ってください。</ p >
292
339
293
340
< script type ="text/javascript ">
294
341
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments