Skip to content

Commit 1d4223b

Browse files
committed
generate
1 parent 90ea4ff commit 1d4223b

File tree

1 file changed

+92
-49
lines changed

1 file changed

+92
-49
lines changed

public/1.6/book/raw-pointers.html

Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta name="generator" content="rustdoc">
7-
<title>Raw Pointers</title>
7+
<title>生ポインタ</title>
88

99
<link rel="stylesheet" type="text/css" href="rustbook.css">
1010

@@ -185,39 +185,60 @@
185185
<div id='page'>
186186

187187

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
190192
there are two types that are extra-special. Much of Rust’s safety comes from
191193
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>
193197

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
195199
writing certain kinds of libraries, you’ll need to get around Rust’s safety
196200
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, `*`
198202
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&lt;T&gt;</code>
200-
and <code>Arc&lt;T&gt;</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&lt;T&gt;</code><code>Arc&lt;T&gt;</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 `&`); -->
201215

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`. -->
204227

205228
<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>&amp;</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&#39;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>&amp;</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>&amp;</code> では保証される)</li>
230+
<li><code>Box</code> とは異なり、自動的な後処理が一切行われないため、手動のリソース管理が必要</li>
231+
<li>plain-old-dataであるため、Rustコンパイラはuse-after-freeのようなバグから保護できない</li>
232+
<li><code>&amp;</code> と異なり、ライフタイムの機能が無効化されるため、コンパイラはダングリングポインタを推論できない</li>
233+
<li>また、 <code>*const T</code> を直接介した変更は拒むが、それ以外のエイリアシングやミュータビリティに関する保証はない</li>
217234
</ul>
218235

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>
221242
<span class='rusttest'>fn main() {
222243
let x = 5;
223244
let raw = &amp;x as *const i32;
@@ -231,7 +252,9 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
231252
<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>;
232253
<span class='kw'>let</span> <span class='ident'>raw_mut</span> <span class='op'>=</span> <span class='kw-2'>&amp;</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>
233254

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>
235258
<span class='rusttest'>fn main() {
236259
let x = 5;
237260
let raw = &amp;x as *const i32;
@@ -243,15 +266,19 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
243266

244267
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;raw points at {}&quot;</span>, <span class='op'>*</span><span class='ident'>raw</span>);</pre>
245268

246-
<p>It gives this error:</p>
269+
<!-- It gives this error: -->
270+
271+
<p>このコードは以下のエラーが発生します。</p>
247272

248273
<pre><code class="language-text">error: dereference of raw pointer requires unsafe function or block [E0133]
249274
println!(&quot;raw points at {}&quot;, *raw);
250275
^~~~
251276
</code></pre>
252277

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>
255282
<span class='rusttest'>fn main() {
256283
let x = 5;
257284
let raw = &amp;x as *const i32;
@@ -267,35 +294,49 @@ <h1 id='basics' class='section-header'><a href='#basics'>Basics</a></h1>
267294

268295
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;raw points at {}&quot;</span>, <span class='ident'>points_at</span>);</pre>
269296

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>
271300

272301
<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>&amp;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>&amp;</code>, is not safe. A
285-
<code>&amp;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>&amp;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
287322
satisfy the aliasing and mutability laws of references. The compiler assumes
288323
these properties are true for any references, no matter how they are created,
289324
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>&amp;</code> へ遡るのは安全ではありません。 <code>&amp;T</code> は常に有効であるため、最低でも <code>*const T</code> は型 <code>T</code> の有効な実体を指さなければならないのです。その上、ポインタは参照のエイリアシングとミュータビリティの規則も満たす必要があります。コンパイラはあらゆる参照についてこれらの性質が真であると仮定しており、その生成方法に依らず適用するため、生ポインタからのいかなる変換も、参照先の値が上記の性質を満たすと表明していることになります。プログラマがこのことを保証 <em>しなければならない</em> のです。</p>
291328

292-
<p>The recommended method for the conversion is:</p>
329+
<!-- The recommended method for the conversion is: -->
330+
331+
<p>おすすめの変換の方法は以下のとおりです。</p>
293332
<span class='rusttest'>fn main() {
294333
// explicit cast
334+
// 明示的キャスト
295335
let i: u32 = 1;
296336
let p_imm: *const u32 = &amp;i as *const u32;
297337

298338
// implicit coercion
339+
// 暗黙的キャスト
299340
let mut m: u32 = 2;
300341
let p_mut: *mut u32 = &amp;mut m;
301342

@@ -304,11 +345,11 @@ <h1 id='references-and-raw-pointers' class='section-header'><a href='#references
304345
let ref_mut: &amp;mut u32 = &amp;mut *p_mut;
305346
}
306347
}</span><pre class='rust rust-example-rendered'>
307-
<span class='comment'>// explicit cast</span>
348+
<span class='comment'>// 明示的キャスト</span>
308349
<span class='kw'>let</span> <span class='ident'>i</span>: <span class='ident'>u32</span> <span class='op'>=</span> <span class='number'>1</span>;
309350
<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'>&amp;</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>;
310351

311-
<span class='comment'>// implicit coercion</span>
352+
<span class='comment'>// 暗黙的キャスト</span>
312353
<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>;
313354
<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'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>m</span>;
314355

@@ -317,10 +358,12 @@ <h1 id='references-and-raw-pointers' class='section-header'><a href='#references
317358
<span class='kw'>let</span> <span class='ident'>ref_mut</span>: <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>u32</span> <span class='op'>=</span> <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='op'>*</span><span class='ident'>p_mut</span>;
318359
}</pre>
319360

320-
<p>The <code>&amp;*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
321362
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>&amp;*x</code> 参照外し方式は <code>transmute</code> を用いるよりも好ましいです。後者は必要以上に強力ですから、より用途が限定されている操作の方が間違って使いにくいでしょう。例えば、前者の方法は <code>x</code> がポインタである必要があります。( <code>transmute</code> とは異なります)</p>
324367

325368
<script type="text/javascript">
326369
window.playgroundUrl = "https://play.rust-lang.org";

0 commit comments

Comments
 (0)