Skip to content

Commit 059dd98

Browse files
committed
Merge pull request #42 from pocket7878/drop
Drop
2 parents 864313b + 1d2b66c commit 059dd98

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

1.6/ja/book/drop.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
% Drop
2+
<!-- % Drop -->
23

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` トレイトは値がスコープ外になった時にコードを実行する方法を提供します:
69

7-
[drop]: ../std/ops/trait.Drop.html
10+
[ドロップ]: ../std/ops/trait.Drop.html
811

912
```rust
1013
struct HasDrop;
@@ -18,18 +21,24 @@ impl Drop for HasDrop {
1821
fn main() {
1922
let x = HasDrop;
2023

21-
// do stuff
24+
# // do stuff
25+
// いくつかの処理
2226

23-
} // x goes out of scope here
27+
# // } // x goes out of scope here
28+
} // x はここでスコープ外になります
2429
```
2530

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` への参照を引数に取るメソッドを持っています。
2936

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+
たとえば、値がドロップされる順序は、それらが定義された順序と反対の順序になります:
3342

3443
```rust
3544
struct Firework {
@@ -48,20 +57,26 @@ fn main() {
4857
}
4958
```
5059

51-
This will output:
60+
<!-- This will output: -->
61+
このコードは以下の様な出力をします:
5262

5363
```text
5464
BOOM times 100!!!
5565
BOOM times 1!!!
5666
```
5767

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になっていたら、内包している値がクリーンアップされます。
6681

6782
[arc]: ../std/sync/struct.Arc.html

0 commit comments

Comments
 (0)