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 > Raw Pointers </ title >
7
+ < title > 生ポインタ </ 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 "> Raw Pointers</ h1 >
189
- < p > Rust has a number of different smart pointer types in its standard library, but
188
+ < h1 class ="title "> 生ポインタ</ h1 >
189
+ <!-- % Raw Pointers -->
190
+
191
+ <!-- Rust has a number of different smart pointer types in its standard library, but
190
192
there are two types that are extra-special. Much of Rust’s safety comes from
191
193
compile-time checks, but raw pointers don’t have such guarantees, and are
192
- < a href ="unsafe.html "> unsafe</ a > to use.</ p >
194
+ [unsafe][unsafe] to use. -->
195
+
196
+ < p > Rustは標準ライブラリに異なるスマートポインタの型を幾つか用意していますが、更に特殊な型が2つあります。Rustの安全性の多くはコンパイル時のチェックに依るものですが、生ポインタを用いるとそういった保証が得られないため < a href ="unsafe.html "> unsafe</ a > です。</ p >
193
197
194
- < p > < code > *const T</ code > and < code > *mut T</ code > are called ‘raw pointers’ in Rust. Sometimes, when
198
+ <!-- ` *const T` and ` *mut T` are called ‘raw pointers’ in Rust. Sometimes, when
195
199
writing certain kinds of libraries, you’ll need to get around Rust’s safety
196
200
guarantees for some reason. In this case, you can use raw pointers to implement
197
- your library, while exposing a safe interface for your users. For example, < code > * </ code >
201
+ your library, while exposing a safe interface for your users. For example, `*`
198
202
pointers are allowed to alias, allowing them to be used to write
199
- shared-ownership types, and even thread-safe shared memory types (the < code > Rc<T></ code >
200
- and < code > Arc<T></ code > types are both implemented entirely in Rust).</ p >
203
+ shared-ownership types, and even thread-safe shared memory types (the `Rc<T>`
204
+ and `Arc<T>` types are both implemented entirely in Rust). -->
205
+
206
+ < p > < code > *const T</ code > と < code > *mut T</ code > はRustにおいて「生ポインタ」と呼ばれます。時々、ある種ののライブラリを書く際に、あなたは何らかの理由でRustが行う安全性の保証を避けなければならないこともあります。このようなケースでは、ユーザに安全なインターフェースを提供しつつ、ライブラリの実装に生ポインタを使用できます。例えば、 < code > *</ code > ポインタはエイリアスとして振る舞うこともできるので、所有権を共有する型を書くのに用いたり、スレッドセーフな共有メモリ型でさえも実装できます。( < code > Rc<T></ code > と < code > Arc<T></ code > 型は完全にRustのみで実装されています)</ p >
207
+
208
+ <!-- Here are some things to remember about raw pointers that are different than
209
+ other pointer types. They: -->
210
+
211
+ < p > 以下は覚えておくべき生ポインタとその他のポインタ型との違いです。</ p >
212
+
213
+ <!-- - are not guaranteed to point to valid memory and are not even
214
+ guaranteed to be non-null (unlike both `Box` and `&`); -->
201
215
202
- < p > Here are some things to remember about raw pointers that are different than
203
- other pointer types. They:</ p >
216
+ <!-- - do not have any automatic clean-up, unlike `Box`, and so require
217
+ manual resource management; -->
218
+
219
+ <!-- - are plain-old-data, that is, they don't move ownership, again unlike
220
+ `Box`, hence the Rust compiler cannot protect against bugs like
221
+ use-after-free; -->
222
+
223
+ <!-- - lack any form of lifetimes, unlike `&`, and so the compiler cannot
224
+ reason about dangling pointers; and
225
+ - have no guarantees about aliasing or mutability other than mutation
226
+ not being allowed directly through a `*const T`. -->
204
227
205
228
< ul >
206
- < li > are not guaranteed to point to valid memory and are not even
207
- guaranteed to be non-null (unlike both < code > Box</ code > and < code > &</ code > );</ li >
208
- < li > do not have any automatic clean-up, unlike < code > Box</ code > , and so require
209
- manual resource management;</ li >
210
- < li > are plain-old-data, that is, they don't move ownership, again unlike
211
- < code > Box</ code > , hence the Rust compiler cannot protect against bugs like
212
- use-after-free;</ li >
213
- < li > lack any form of lifetimes, unlike < code > &</ code > , and so the compiler cannot
214
- reason about dangling pointers; and</ li >
215
- < li > have no guarantees about aliasing or mutability other than mutation
216
- not being allowed directly through a < code > *const T</ code > .</ li >
229
+ < li > 有効なメモリを指していることが保証されないどころか、nullでないことも保証されない( < code > Box</ code > と < code > &</ code > では保証される)</ li >
230
+ < li > < code > Box</ code > とは異なり、自動的な後処理が一切行われないため、手動のリソース管理が必要</ li >
231
+ < li > plain-old-dataであるため、Rustコンパイラはuse-after-freeのようなバグから保護できない</ li >
232
+ < li > < code > &</ code > と異なり、ライフタイムの機能が無効化されるため、コンパイラはダングリングポインタを推論できない</ li >
233
+ < li > また、 < code > *const T</ code > を直接介した変更は拒むが、それ以外のエイリアシングやミュータビリティに関する保証はない</ li >
217
234
</ ul >
218
235
219
- < h1 id ='basics ' class ='section-header '> < a href ='#basics '> Basics</ a > </ h1 >
220
- < p > Creating a raw pointer is perfectly safe:</ p >
236
+ <!-- # Basics -->
237
+
238
+ < h1 id ='基本 ' class ='section-header '> < a href ='#基本 '> 基本</ a > </ h1 >
239
+ <!-- Creating a raw pointer is perfectly safe: -->
240
+
241
+ < p > 生ポインタを作成すること自体は絶対に安全です。</ p >
221
242
< span class ='rusttest '> fn main() {
222
243
let x = 5;
223
244
let raw = &x as *const i32;
@@ -231,7 +252,9 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
231
252
< span class ='kw '> let</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> y</ span > < span class ='op '> =</ span > < span class ='number '> 10</ span > ;
232
253
< span class ='kw '> let</ span > < span class ='ident '> raw_mut</ span > < span class ='op '> =</ span > < span class ='kw-2 '> &</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> y</ span > < span class ='kw '> as</ span > < span class ='op '> *</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> i32</ span > ;</ pre >
233
254
234
- < p > However, dereferencing one is not. This won’t work:</ p >
255
+ <!-- However, dereferencing one is not. This won’t work: -->
256
+
257
+ < p > しかしながら参照外しは安全ではありません。以下は動作しないでしょう。</ p >
235
258
< span class ='rusttest '> fn main() {
236
259
let x = 5;
237
260
let raw = &x as *const i32;
@@ -243,15 +266,19 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
243
266
244
267
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "raw points at {}"</ span > , < span class ='op '> *</ span > < span class ='ident '> raw</ span > );</ pre >
245
268
246
- < p > It gives this error:</ p >
269
+ <!-- It gives this error: -->
270
+
271
+ < p > このコードは以下のエラーが発生します。</ p >
247
272
248
273
< pre > < code class ="language-text "> error: dereference of raw pointer requires unsafe function or block [E0133]
249
274
println!("raw points at {}", *raw);
250
275
^~~~
251
276
</ code > </ pre >
252
277
253
- < p > When you dereference a raw pointer, you’re taking responsibility that it’s not
254
- pointing somewhere that would be incorrect. As such, you need < code > unsafe</ code > :</ p >
278
+ <!-- When you dereference a raw pointer, you’re taking responsibility that it’s not
279
+ pointing somewhere that would be incorrect. As such, you need `unsafe`: -->
280
+
281
+ < p > 生ポインタを参照外しする時、ポインタが間違った場所を指していないことに対して責任を負うことになります。そういう時は、 < code > unsafe</ code > を付けなければなりません。</ p >
255
282
< span class ='rusttest '> fn main() {
256
283
let x = 5;
257
284
let raw = &x as *const i32;
@@ -267,35 +294,49 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
267
294
268
295
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "raw points at {}"</ span > , < span class ='ident '> points_at</ span > );</ pre >
269
296
270
- < p > For more operations on raw pointers, see < a href ="../std/primitive.pointer.html "> their API documentation</ a > .</ p >
297
+ <!-- For more operations on raw pointers, see [their API documentation][rawapi]. -->
298
+
299
+ < p > 生ポインタの操作に関する詳細は、 < a href ="../std/primitive.pointer.html "> APIドキュメント</ a > を参照してください。</ p >
271
300
272
301
< h1 id ='ffi ' class ='section-header '> < a href ='#ffi '> FFI</ a > </ h1 >
273
- < p > Raw pointers are useful for FFI: Rust’s < code > *const T</ code > and < code > *mut T</ code > are similar to
274
- C’s < code > const T*</ code > and < code > T*</ code > , respectively. For more about this use, consult the
275
- < a href ="ffi.html "> FFI chapter</ a > .</ p >
276
-
277
- < h1 id ='references-and-raw-pointers ' class ='section-header '> < a href ='#references-and-raw-pointers '> References and raw pointers</ a > </ h1 >
278
- < p > At runtime, a raw pointer < code > *</ code > and a reference pointing to the same piece of
279
- data have an identical representation. In fact, an < code > &T</ code > reference will
280
- implicitly coerce to an < code > *const T</ code > raw pointer in safe code and similarly for
281
- the < code > mut</ code > variants (both coercions can be performed explicitly with,
282
- respectively, < code > value as *const T</ code > and < code > value as *mut T</ code > ).</ p >
283
-
284
- < p > Going the opposite direction, from < code > *const</ code > to a reference < code > &</ code > , is not safe. A
285
- < code > &T</ code > is always valid, and so, at a minimum, the raw pointer < code > *const T</ code > has to
286
- point to a valid instance of type < code > T</ code > . Furthermore, the resulting pointer must
302
+ <!-- Raw pointers are useful for FFI: Rust’s `*const T` and `*mut T` are similar to
303
+ C’s `const T*` and `T*`, respectively. For more about this use, consult the
304
+ [FFI chapter][ffi]. -->
305
+
306
+ < p > 生ポインタはFFIを使う際に役立ちます。Rustの < code > *const T</ code > と < code > *mut T</ code > はそれぞれC言語の < code > const T*</ code > と < code > T*</ code > に似ているからです。これの使い方に関する詳細は、 < a href ="ffi.html "> FFIの章</ a > を参照してください。</ p >
307
+
308
+ <!-- # References and raw pointers -->
309
+
310
+ < h1 id ='参照と生ポインタ ' class ='section-header '> < a href ='#参照と生ポインタ '> 参照と生ポインタ</ a > </ h1 >
311
+ <!-- At runtime, a raw pointer `*` and a reference pointing to the same piece of
312
+ data have an identical representation. In fact, an `&T` reference will
313
+ implicitly coerce to an `*const T` raw pointer in safe code and similarly for
314
+ the `mut` variants (both coercions can be performed explicitly with,
315
+ respectively, `value as *const T` and `value as *mut T`). -->
316
+
317
+ < p > 実行時において、同じデータを指す生ポインタ < code > *</ code > と参照は内部的に同一です。事実、 < code > unsafe</ code > 外の安全なコードにおいて < code > &T</ code > 参照は < code > *const T</ code > 生ポインタへ暗黙的に型強制されますし、 < code > mut</ code > の場合でも同様です。(これら型強制は、それぞれ < code > value as *const T</ code > と < code > value as *mut T</ code > のように、明示的に行うこともできます。)</ p >
318
+
319
+ <!-- Going the opposite direction, from `*const` to a reference `&`, is not safe. A
320
+ `&T` is always valid, and so, at a minimum, the raw pointer `*const T` has to
321
+ point to a valid instance of type `T`. Furthermore, the resulting pointer must
287
322
satisfy the aliasing and mutability laws of references. The compiler assumes
288
323
these properties are true for any references, no matter how they are created,
289
324
and so any conversion from raw pointers is asserting that they hold. The
290
- programmer < em > must</ em > guarantee this.</ p >
325
+ programmer *must* guarantee this. -->
326
+
327
+ < p > 逆に、 < code > *const</ code > から 参照 < code > &</ code > へ遡るのは安全ではありません。 < code > &T</ code > は常に有効であるため、最低でも < code > *const T</ code > は型 < code > T</ code > の有効な実体を指さなければならないのです。その上、ポインタは参照のエイリアシングとミュータビリティの規則も満たす必要があります。コンパイラはあらゆる参照についてこれらの性質が真であると仮定しており、その生成方法に依らず適用するため、生ポインタからのいかなる変換も、参照先の値が上記の性質を満たすと表明していることになります。プログラマがこのことを保証 < em > しなければならない</ em > のです。</ p >
291
328
292
- < p > The recommended method for the conversion is:</ p >
329
+ <!-- The recommended method for the conversion is: -->
330
+
331
+ < p > おすすめの変換の方法は以下のとおりです。</ p >
293
332
< span class ='rusttest '> fn main() {
294
333
// explicit cast
334
+ // 明示的キャスト
295
335
let i: u32 = 1;
296
336
let p_imm: *const u32 = &i as *const u32;
297
337
298
338
// implicit coercion
339
+ // 暗黙的キャスト
299
340
let mut m: u32 = 2;
300
341
let p_mut: *mut u32 = &mut m;
301
342
@@ -304,11 +345,11 @@ <h1 id='references-and-raw-pointers' class='section-header'><a href='#references
304
345
let ref_mut: &mut u32 = &mut *p_mut;
305
346
}
306
347
}</ span > < pre class ='rust rust-example-rendered '>
307
- < span class ='comment '> // explicit cast </ span >
348
+ < span class ='comment '> // 明示的キャスト </ span >
308
349
< span class ='kw '> let</ span > < span class ='ident '> i</ span > : < span class ='ident '> u32</ span > < span class ='op '> =</ span > < span class ='number '> 1</ span > ;
309
350
< span class ='kw '> let</ span > < span class ='ident '> p_imm</ span > : < span class ='op '> *</ span > < span class ='kw '> const</ span > < span class ='ident '> u32</ span > < span class ='op '> =</ span > < span class ='kw-2 '> &</ span > < span class ='ident '> i</ span > < span class ='kw '> as</ span > < span class ='op '> *</ span > < span class ='kw '> const</ span > < span class ='ident '> u32</ span > ;
310
351
311
- < span class ='comment '> // implicit coercion </ span >
352
+ < span class ='comment '> // 暗黙的キャスト </ span >
312
353
< span class ='kw '> let</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> m</ span > : < span class ='ident '> u32</ span > < span class ='op '> =</ span > < span class ='number '> 2</ span > ;
313
354
< span class ='kw '> let</ span > < span class ='ident '> p_mut</ span > : < span class ='op '> *</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> u32</ span > < span class ='op '> =</ span > < span class ='kw-2 '> &</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> m</ span > ;
314
355
@@ -317,10 +358,12 @@ <h1 id='references-and-raw-pointers' class='section-header'><a href='#references
317
358
< span class ='kw '> let</ span > < span class ='ident '> ref_mut</ span > : < span class ='kw-2 '> &</ span > < span class ='kw-2 '> mut</ span > < span class ='ident '> u32</ span > < span class ='op '> =</ span > < span class ='kw-2 '> &</ span > < span class ='kw-2 '> mut</ span > < span class ='op '> *</ span > < span class ='ident '> p_mut</ span > ;
318
359
}</ pre >
319
360
320
- < p > The < code > &*x </ code > dereferencing style is preferred to using a < code > transmute</ code > . The latter
361
+ <!-- The `&*x` dereferencing style is preferred to using a ` transmute` . The latter
321
362
is far more powerful than necessary, and the more restricted operation is
322
- harder to use incorrectly; for example, it requires that < code > x</ code > is a pointer
323
- (unlike < code > transmute</ code > ).</ p >
363
+ harder to use incorrectly; for example, it requires that `x` is a pointer
364
+ (unlike `transmute`). -->
365
+
366
+ < p > < code > &*x</ code > 参照外し方式は < code > transmute</ code > を用いるよりも好ましいです。後者は必要以上に強力ですから、より用途が限定されている操作の方が間違って使いにくいでしょう。例えば、前者の方法は < code > x</ code > がポインタである必要があります。( < code > transmute</ code > とは異なります)</ p >
324
367
325
368
< script type ="text/javascript ">
326
369
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments