Skip to content

Drop #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2016
Merged

Drop #42

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions 1.6/ja/book/drop.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
% Drop
<!-- % Drop -->

Now that we’ve discussed traits, let’s talk about a particular trait provided
by the Rust standard library, [`Drop`][drop]. The `Drop` trait provides a way
to run some code when a value goes out of scope. For example:
<!-- Now that we’ve discussed traits, let’s talk about a particular trait provided -->
<!-- by the Rust standard library, [`Drop`][drop]. The `Drop` trait provides a way -->
<!-- to run some code when a value goes out of scope. For example: -->
トレイトについて学びましたので、Rustの標準ライブラリによって提供されている具体的なトレイト [`Drop`][drop]について説明しましょう。
`Drop` トレイトは値がスコープ外になった時にコードを実行する方法を提供します:

[drop]: ../std/ops/trait.Drop.html
[ドロップ]: ../std/ops/trait.Drop.html

```rust
struct HasDrop;
Expand All @@ -18,18 +21,24 @@ impl Drop for HasDrop {
fn main() {
let x = HasDrop;

// do stuff
# // do stuff
// いくつかの処理

} // x goes out of scope here
# // } // x goes out of scope here
} // x はここでスコープ外になります
```

When `x` goes out of scope at the end of `main()`, the code for `Drop` will
run. `Drop` has one method, which is also called `drop()`. It takes a mutable
reference to `self`.
<!-- When `x` goes out of scope at the end of `main()`, the code for `Drop` will -->
<!-- run. `Drop` has one method, which is also called `drop()`. It takes a mutable -->
<!-- reference to `self`. -->
`x` が `main()` の終わりでスコープ外になった時、 `Drop` のコードが実行されます。
`Drop` は `drop()` と呼ばれるミュータブルな `self` への参照を引数に取るメソッドを持っています。

That’s it! The mechanics of `Drop` are very simple, but there are some
subtleties. For example, values are dropped in the opposite order they are
declared. Here’s another example:
<!-- That’s it! The mechanics of `Drop` are very simple, but there are some -->
<!-- subtleties. For example, values are dropped in the opposite order they are -->
<!-- declared. Here’s another example: -->
これだけです! `Drop` のメカニズムは非常にシンプルです、しかし少しだけ注意すべき点があります。
たとえば、値がドロップされる順序は、それらが定義された順序と反対の順序になります:

```rust
struct Firework {
Expand All @@ -48,20 +57,26 @@ fn main() {
}
```

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

```text
BOOM times 100!!!
BOOM times 1!!!
```

The TNT goes off before the firecracker does, because it was declared
afterwards. Last in, first out.

So what is `Drop` good for? Generally, `Drop` is used to clean up any resources
associated with a `struct`. For example, the [`Arc<T>` type][arc] is a
reference-counted type. When `Drop` is called, it will decrement the reference
count, and if the total number of references is zero, will clean up the
underlying value.
<!-- The TNT goes off before the firecracker does, because it was declared -->
<!-- afterwards. Last in, first out. -->
TNTが爆竹(firecracker)が鳴る前に爆発してしまいました、これはTNTが定義されたのは爆竹よりも後だったことによります。
ラストイン・ファーストアウトです。

<!-- So what is `Drop` good for? Generally, `Drop` is used to clean up any resources -->
<!-- associated with a `struct`. For example, the [`Arc<T>` type][arc] is a -->
<!-- reference-counted type. When `Drop` is called, it will decrement the reference -->
<!-- count, and if the total number of references is zero, will clean up the -->
<!-- underlying value. -->
`Drop` は何をするのに適しているのでしょうか?一般的に `Drop` は `struct` に関連付けられているリソースのクリーンアップに利用されます。
たとえば、 [`Arc<T>` 型][arc] は参照カウントを行う型です。 `Drop` が呼ばれると、参照カウントがデクリメントされ、
もし参照の合計数が0になっていたら、内包している値がクリーンアップされます。

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