@@ -8,7 +8,6 @@ of programs, we need to tell the compiler to relax its restrictions a bit. For
8
8
this, Rust has a keyword, `unsafe`. Code using `unsafe` has less restrictions
9
9
than normal code does.
10
10
-->
11
-
12
11
Rustの主たる魅力は、プログラムの動作についての強力で静的な保証です。
13
12
しかしながら、安全性検査は本来保守的なものです。
14
13
すなわち、実際には安全なのに、そのことがコンパイラには検証できないプログラムがいくらか存在します。
@@ -20,7 +19,6 @@ Rustの主たる魅力は、プログラムの動作についての強力で静
20
19
Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in
21
20
four contexts. The first one is to mark a function as unsafe:
22
21
-->
23
-
24
22
まずシンタックスをみて、それからセマンティクスについて話しましょう。
25
23
` unsafe ` は4つの場面で使われます。
26
24
1つめは、関数がアンセーフであることを印付ける場合です。
@@ -36,7 +34,6 @@ unsafe fn danger_will_robinson() {
36
34
All functions called from [FFI][ffi] must be marked as `unsafe`, for example.
37
35
The second use of `unsafe` is an unsafe block:
38
36
-->
39
-
40
37
たとえば、[ FFI] [ ffi ] から呼び出されるすべての関数は` unsafe ` で印付けることが必要です。
41
38
` unsafe ` の2つめの用途は、アンセーフブロックです。
42
39
@@ -50,15 +47,13 @@ unsafe {
50
47
```
51
48
52
49
<!-- The third is for unsafe traits:-->
53
-
54
50
3つめは、アンセーフトレイトです。
55
51
56
52
``` rust
57
53
unsafe trait Scary { }
58
54
```
59
55
60
56
<!-- And the fourth is for `impl`ementing one of those traits:-->
61
-
62
57
そして、4つめは、そのアンセーフトレイトを実装する場合です。
63
58
64
59
``` rust
@@ -71,7 +66,6 @@ It’s important to be able to explicitly delineate code that may have bugs that
71
66
cause big problems. If a Rust program segfaults, you can be sure it’s somewhere
72
67
in the sections marked `unsafe`.
73
68
-->
74
-
75
69
大きな問題を引き起こすバグがあるかもしれないコードを明示できるのは重要なことです。
76
70
もしRustのプログラムがセグメンテーション違反を起こしても、バグは ` unsafe ` で印付けられた区間のどこかにあると確信できます。
77
71
@@ -83,7 +77,6 @@ Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s a
83
77
important to know that there are certain behaviors that are probably not
84
78
desirable in your code, but are expressly _not_ unsafe:
85
79
-->
86
-
87
80
Rustの文脈で、安全とは「どのようなアンセーフなこともしない」ことを意味します。
88
81
89
82
> 訳注:
@@ -110,7 +103,6 @@ Rust cannot prevent all kinds of software problems. Buggy code can and will be
110
103
written in Rust. These things aren’t great, but they don’t qualify as `unsafe`
111
104
specifically.
112
105
-->
113
-
114
106
Rustはソフトウェアが抱えるすべての種類の問題を防げるわけではありません。
115
107
Rustでバグのあるコードを書くことはできますし、実際に書かれるでしょう。
116
108
これらの動作は良いことではありませんが、特にアンセーフだとは見なされません。
@@ -119,7 +111,6 @@ Rustでバグのあるコードを書くことはできますし、実際に書
119
111
In addition, the following are all undefined behaviors in Rust, and must be
120
112
avoided, even when writing `unsafe` code:
121
113
-->
122
-
123
114
さらに、Rustにおいては、次のものは未定義動作で、 ` unsafe ` コード中であっても、避ける必要があります。
124
115
125
116
> 訳注:
@@ -183,15 +174,13 @@ avoided, even when writing `unsafe` code:
183
174
In both unsafe functions and unsafe blocks, Rust will let you do three things
184
175
that you normally can not do. Just three. Here they are:
185
176
-->
186
-
187
177
アンセーフ関数・アンセーフブロックでは、Rustは普段できない3つのことをさせてくれます。たった3つです。それは、
188
178
189
179
<!--
190
180
1. Access or update a [static mutable variable][static].
191
181
2. Dereference a raw pointer.
192
182
3. Call unsafe functions. This is the most powerful ability.
193
183
-->
194
-
195
184
1 . [ 静的ミュータブル変数] [ static ] のアクセスとアップデート。
196
185
2 . 生ポインタの参照外し。
197
186
3 . アンセーフ関数の呼び出し。これが最も強力な能力です。
@@ -202,7 +191,6 @@ borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its
202
191
semantics, it won’t just start accepting anything. But it will let you write
203
192
things that _do_ break some of the rules.
204
193
-->
205
-
206
194
以上です。
207
195
重要なのは、 ` unsafe ` が、たとえば「借用チェッカをオフにする」といったことを行わないことです。
208
196
Rustのコードの適当な位置に ` unsafe ` を加えてもセマンティクスは変わらず、何でもただ受理するようになるということにはなりません。
@@ -213,14 +201,12 @@ You will also encounter the `unsafe` keyword when writing bindings to foreign
213
201
(non-Rust) interfaces. You're encouraged to write a safe, native Rust interface
214
202
around the methods provided by the library.
215
203
-->
216
-
217
204
また、` unsafe ` キーワードは、Rust以外の言語とのインターフェースを書くときに遭遇するでしょう。
218
205
ライブラリの提供するメソッドの周りに、安全な、Rustネイティブのインターフェースを書くことが推奨されています。
219
206
220
207
<!--
221
208
Let’s go over the basic three abilities listed, in order.
222
209
-->
223
-
224
210
これから、その基本的な3つの能力を順番に見ていきましょう。
225
211
226
212
## ` static mut ` のアクセスとアップデート。
@@ -231,7 +217,6 @@ Rust has a feature called ‘`static mut`’ which allows for mutable global sta
231
217
Doing so can cause a data race, and as such is inherently not safe. For more
232
218
details, see the [static][static] section of the book.
233
219
-->
234
-
235
220
Rustには「` static mut ` 」という、ミュータブルでグローバルな状態を実現する機能があります。
236
221
これを使うことはデータレースが起こるおそれがあるので、本質的に安全ではありません。
237
222
詳細は、この本の[ static] [ static ] セクションを参照してください。
@@ -247,7 +232,6 @@ different memory safety and security issues. In some senses, the ability to
247
232
dereference an arbitrary pointer is one of the most dangerous things you can
248
233
do. For more on raw pointers, see [their section of the book][rawpointers].
249
234
-->
250
-
251
235
生ポインタによって任意のポインタ演算が可能になりますが、いくつもの異なるメモリ安全とセキュリティの問題が起こるおそれがあります。
252
236
ある意味で、任意のポインタを参照外しする能力は行いうる操作のうち最も危険なもののひとつです。
253
237
詳細は、[ この本の生ポインタに関するセクション] [ rawpointers ] を参照してください。
@@ -261,7 +245,6 @@ do. For more on raw pointers, see [their section of the book][rawpointers].
261
245
This last ability works with both aspects of `unsafe`: you can only call
262
246
functions marked `unsafe` from inside an unsafe block.
263
247
-->
264
-
265
248
この最後の能力は、` unsafe ` の両面とともに働きます。
266
249
すなわち、` unsafe ` で印付けられた関数は、アンセーフブロックの内部からのみ呼び出すことができます。
267
250
@@ -270,7 +253,6 @@ This ability is powerful and varied. Rust exposes some [compiler
270
253
intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass
271
254
safety checks, trading safety for speed.
272
255
-->
273
-
274
256
この能力は強力で多彩です。
275
257
Rustはいくらかの[ compiler intrinsics] [ intrinsics ] をアンセーフ関数として公開しており、また、いくつかのアンセーフ関数は安全性検査を回避することで、安全性とスピードを引き換えています。
276
258
@@ -279,7 +261,6 @@ I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks
279
261
and functions doesn’t mean you should. The compiler will act as though you’re
280
262
upholding its invariants, so be careful!
281
263
-->
282
-
283
264
繰り返しになりますが、アンセーフブロックと関数の内部で任意のことが _ できる_ としても、それをすべきだということを意味しません。コンパイラは、あなたが不変量を守っているかのように動作しますから、注意してください!
284
265
285
266
[ intrinsics ] : intrinsics.html
0 commit comments