From 9f00d89dd1f4b900d95048077dba5acf81bca217 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 06:55:05 +0800 Subject: [PATCH 1/4] 16.8.25.6.50 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 整理Drop --- src/drop.md | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/drop.md b/src/drop.md index 7dd3db0..873e2c1 100644 --- a/src/drop.md +++ b/src/drop.md @@ -1,8 +1,8 @@ -% 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: +基於我們已經討論過特征了,現在讓我們一起來了解由Rust語言標準庫提供的特殊 +的特征—— [`Drop`(丟棄)][drop]. 丟棄的特征是由一個值離開作用域時觸發的方 +法,譬如說: [drop]: ../std/ops/trait.Drop.html @@ -23,13 +23,11 @@ fn main() { } // x goes out of scope here ``` -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: +嗚呼! 丟棄 `Drop` 的運行原理非常簡單。只是還有一些細節問題:譬 +如說值會被它們聲明的相反順序觸發丟棄的過程,譬如說: ```rust struct Firework { @@ -48,21 +46,19 @@ fn main() { } ``` -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. +由於TNT在firecracker之後被聲明,所以TNT在firecracker之前離開作用域。 + +那麼 `Drop` 有什麼好處呢?通常情況下 `Drop` 被用來清理和 `struct`關聯得到資源。 +譬如說 [`Arc` 型別][arc] 是一個引用計數型別。當`Drop`被調用的時候,它就會減 +少引用計數。此外,如果引用的總數為零,底層的值將會被擦除。 -So what is `Drop` good for? Generally, `Drop` is used to clean up any resources -associated with a `struct`. For example, the [`Arc` 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. [arc]: ../std/sync/struct.Arc.html From e74e7e2bfc717d0f75390b554b784fcd6f4a52a5 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 08:56:54 +0800 Subject: [PATCH 2/4] 16.8.25.8.56 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新目錄 --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7962f42..bfa291f 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -23,7 +23,7 @@ * [字串](strings.md) * [泛型](generics.md) * [Traits](traits.md) - * [Drop](drop.md) + * [丟棄](drop.md) * [if let](if-let.md) * [Trait Objects](trait-objects.md) * [Closures](closures.md) From a1598f7b9ac315dab6461c5016aebf863e404c05 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Fri, 26 Aug 2016 11:15:12 +0800 Subject: [PATCH 3/4] 16.8.26.11.14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修整了若干錯別字和排版的問題 --- src/drop.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/drop.md b/src/drop.md index 873e2c1..6fff119 100644 --- a/src/drop.md +++ b/src/drop.md @@ -1,7 +1,7 @@ % 丟棄 -基於我們已經討論過特征了,現在讓我們一起來了解由Rust語言標準庫提供的特殊 -的特征—— [`Drop`(丟棄)][drop]. 丟棄的特征是由一個值離開作用域時觸發的方 +基於我們已經討論過特徵了,現在讓我們一起來了解由Rust語言標準庫提供的特殊 +的特徵—— [`Drop`(丟棄)][drop]. 丟棄的特徵是由一個值離開作用域時觸發的方 法,譬如說: [drop]: ../std/ops/trait.Drop.html @@ -23,8 +23,8 @@ fn main() { } // x goes out of scope here ``` -當變數 `x` 離開它的作用域 `main()`的底部的時候, 丟棄 `Drop`的代碼 -就被觸發了。 丟棄 `Drop`有一個方法也被寫作 `drop()`。它用來回去一個自身`self`的可變引用。 +當變數 `x` 離開它的作用域 `main()` 的底部的時候, 丟棄 `Drop` 的代碼 +就被觸發了。 丟棄 `Drop` 有一個方法也被寫作 `drop()`。它用來回去一個自身 `self` 的可變引用。 嗚呼! 丟棄 `Drop` 的運行原理非常簡單。只是還有一些細節問題:譬 如說值會被它們聲明的相反順序觸發丟棄的過程,譬如說: @@ -53,10 +53,10 @@ BOOM times 100!!! BOOM times 1!!! ``` -由於TNT在firecracker之後被聲明,所以TNT在firecracker之前離開作用域。 +由於 TNT 在 firecracker 之後被聲明,所以 TNT 在 firecracker 之前離開作用域。 -那麼 `Drop` 有什麼好處呢?通常情況下 `Drop` 被用來清理和 `struct`關聯得到資源。 -譬如說 [`Arc` 型別][arc] 是一個引用計數型別。當`Drop`被調用的時候,它就會減 +那麼 `Drop` 有什麼好處呢?通常情況下 `Drop` 被用來清理和 `struct` 關聯得到資源。 +譬如說 [`Arc` 型別][arc] 是一個引用計數型別。當 `Drop` 被調用的時候,它就會減 少引用計數。此外,如果引用的總數為零,底層的值將會被擦除。 From bb872fa4c74c497a64843d0df8eb190523273a94 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 1 Sep 2016 12:24:12 +0800 Subject: [PATCH 4/4] 16.9.1.12.23 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正了若干問題 - 中文排版的問題 - 一些術語 - 還需要進一步確定的翻譯 --- src/drop.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/drop.md b/src/drop.md index 6fff119..a00324f 100644 --- a/src/drop.md +++ b/src/drop.md @@ -1,8 +1,7 @@ % 丟棄 -基於我們已經討論過特徵了,現在讓我們一起來了解由Rust語言標準庫提供的特殊 -的特徵—— [`Drop`(丟棄)][drop]. 丟棄的特徵是由一個值離開作用域時觸發的方 -法,譬如說: +基於我們已經討論過特徵了,現在讓我們一起來了解由Rust語言標準庫提供的特殊的特徵—— [`Drop`(丟棄)][drop]。 +丟棄(drop)的特徵(trait)是由一個值離開作用域時觸發的方法,譬如說: [drop]: ../std/ops/trait.Drop.html @@ -23,11 +22,11 @@ fn main() { } // x goes out of scope here ``` -當變數 `x` 離開它的作用域 `main()` 的底部的時候, 丟棄 `Drop` 的代碼 -就被觸發了。 丟棄 `Drop` 有一個方法也被寫作 `drop()`。它用來回去一個自身 `self` 的可變引用。 +當變數 `x` 離開它的作用域 `main()` 的底部的時候, 丟棄 `Drop` 的代碼就被觸發了。 +丟棄 `Drop` 有一個方法也被寫作 `drop()`。它用來回去一個自身 `self` 的可變引用。 -嗚呼! 丟棄 `Drop` 的運行原理非常簡單。只是還有一些細節問題:譬 -如說值會被它們聲明的相反順序觸發丟棄的過程,譬如說: +正因如此,丟棄 `Drop` 的運行原理非常簡單。只是還有一些細節問題: +譬如說值會被以它們宣告的相反順序觸發丟棄(drop)的過程,譬如說: ```rust struct Firework { @@ -56,8 +55,9 @@ BOOM times 1!!! 由於 TNT 在 firecracker 之後被聲明,所以 TNT 在 firecracker 之前離開作用域。 那麼 `Drop` 有什麼好處呢?通常情況下 `Drop` 被用來清理和 `struct` 關聯得到資源。 -譬如說 [`Arc` 型別][arc] 是一個引用計數型別。當 `Drop` 被調用的時候,它就會減 -少引用計數。此外,如果引用的總數為零,底層的值將會被擦除。 +譬如說 [`Arc` 型別][arc] 是一個引用計數型別。 +當 `Drop` 被調用的時候,它就會減少引用計數。 +此外,如果引用的總數為零,底層的值將會被擦除。 [arc]: ../std/sync/struct.Arc.html