1
1
% Drop
2
+ <!-- % Drop -->
2
3
3
- Now that we’ve discussed traits, let’s talk about a particular trait provided
4
- by the Rust standard library, [ ` Drop ` ] [ drop ] . The ` Drop ` trait provides a way
5
- to run some code when a value goes out of scope. For example:
4
+ <!-- Now that we’ve discussed traits, let’s talk about a particular trait provided -->
5
+ <!-- by the Rust standard library, [`Drop`][drop]. The `Drop` trait provides a way -->
6
+ <!-- to run some code when a value goes out of scope. For example: -->
7
+ トレイトについて学びましたので、Rustの標準ライブラリによって提供されている具体的なトレイト [ ` Drop ` ] [ drop ] について説明しましょう。
8
+ ` Drop ` トレイトは値がスコープ外になった時にコードを実行する方法を提供します:
6
9
7
- [ drop ] : ../std/ops/trait.Drop.html
10
+ [ ドロップ ] : ../std/ops/trait.Drop.html
8
11
9
12
``` rust
10
13
struct HasDrop ;
@@ -18,18 +21,24 @@ impl Drop for HasDrop {
18
21
fn main () {
19
22
let x = HasDrop ;
20
23
21
- // do stuff
24
+ # // do stuff
25
+ // いくつかの処理
22
26
23
- } // x goes out of scope here
27
+ # // } // x goes out of scope here
28
+ } // x はここでスコープ外になります
24
29
```
25
30
26
- When ` x ` goes out of scope at the end of ` main() ` , the code for ` Drop ` will
27
- run. ` Drop ` has one method, which is also called ` drop() ` . It takes a mutable
28
- reference to ` self ` .
31
+ <!-- When `x` goes out of scope at the end of `main()`, the code for `Drop` will -->
32
+ <!-- run. `Drop` has one method, which is also called `drop()`. It takes a mutable -->
33
+ <!-- reference to `self`. -->
34
+ ` x ` が ` main() ` の終わりでスコープ外になった時、 ` Drop ` のコードが実行されます。
35
+ ` Drop ` は ` drop() ` と呼ばれるミュータブルな ` self ` への参照を引数に取るメソッドを持っています。
29
36
30
- That’s it! The mechanics of ` Drop ` are very simple, but there are some
31
- subtleties. For example, values are dropped in the opposite order they are
32
- declared. Here’s another example:
37
+ <!-- That’s it! The mechanics of `Drop` are very simple, but there are some -->
38
+ <!-- subtleties. For example, values are dropped in the opposite order they are -->
39
+ <!-- declared. Here’s another example: -->
40
+ これだけです! ` Drop ` のメカニズムは非常にシンプルです、しかし少しだけ注意すべき点があります。
41
+ たとえば、値がドロップされる順序は、それらが定義された順序と反対の順序になります:
33
42
34
43
``` rust
35
44
struct Firework {
@@ -48,20 +57,26 @@ fn main() {
48
57
}
49
58
```
50
59
51
- This will output:
60
+ <!-- This will output: -->
61
+ このコードは以下の様な出力をします:
52
62
53
63
``` text
54
64
BOOM times 100!!!
55
65
BOOM times 1!!!
56
66
```
57
67
58
- The TNT goes off before the firecracker does, because it was declared
59
- afterwards. Last in, first out.
60
-
61
- So what is ` Drop ` good for? Generally, ` Drop ` is used to clean up any resources
62
- associated with a ` struct ` . For example, the [ ` Arc<T> ` type] [ arc ] is a
63
- reference-counted type. When ` Drop ` is called, it will decrement the reference
64
- count, and if the total number of references is zero, will clean up the
65
- underlying value.
68
+ <!-- The TNT goes off before the firecracker does, because it was declared -->
69
+ <!-- afterwards. Last in, first out. -->
70
+ TNTが爆竹(firecracker)が鳴る前に爆発してしまいました、これはTNTが定義されたのは爆竹よりも後だったことによります。
71
+ ラストイン・ファーストアウトです。
72
+
73
+ <!-- So what is `Drop` good for? Generally, `Drop` is used to clean up any resources -->
74
+ <!-- associated with a `struct`. For example, the [`Arc<T>` type][arc] is a -->
75
+ <!-- reference-counted type. When `Drop` is called, it will decrement the reference -->
76
+ <!-- count, and if the total number of references is zero, will clean up the -->
77
+ <!-- underlying value. -->
78
+ ` Drop ` は何をするのに適しているのでしょうか?一般的に ` Drop ` は ` struct ` に関連付けられているリソースのクリーンアップに利用されます。
79
+ たとえば、 [ ` Arc<T> ` 型] [ arc ] は参照カウントを行う型です。 ` Drop ` が呼ばれると、参照カウントがデクリメントされ、
80
+ もし参照の合計数が0になっていたら、内包している値がクリーンアップされます。
66
81
67
82
[ arc ] : ../std/sync/struct.Arc.html
0 commit comments