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