From 0c93aab1cdc68e453ad85e4b3feae2d1292c9887 Mon Sep 17 00:00:00 2001 From: 3442853561 <3442853561@qq.com> Date: Tue, 23 Aug 2016 12:08:15 +0800 Subject: [PATCH 1/8] 16.8.23.12.7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 翻譯了若干內容16.8.23.12.7 --- src/const-and-static.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 03f072f..2a5dce7 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -1,20 +1,16 @@ -% `const` and `static` +% 常量與靜態量 -Rust has a way of defining constants with the `const` keyword: +在Rust語言中定義常量可以使用 `const` 關鍵字: ```rust const N: i32 = 5; ``` -Unlike [`let`][let] bindings, you must annotate the type of a `const`. +你必須明確一個 `const` 的數據類型,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 [let]: variable-bindings.html -Constants live for the entire lifetime of a program. More specifically, -constants in Rust have no fixed address in memory. This is because they’re -effectively inlined to each place that they’re used. References to the same -constant are not necessarily guaranteed to refer to the same memory address for -this reason. +常量作用於整個程式的生命週期。實際上,在Rust語言中常量在在內存中並沒有確定的地址,它們會被內聯到所有被使用的地方。因此對於同一個常量的引用並不能確保您引用的是同一個內存地址內的數據。 # `static` @@ -23,7 +19,7 @@ similar to constants, but static items aren’t inlined upon use. This means tha there is only one instance for each value, and it’s at a fixed location in memory. -Here’s an example: +這裡有一道例題: ```rust static N: i32 = 5; From 59b31eac819b500deef8ad3fd9b2acb8a9a74e77 Mon Sep 17 00:00:00 2001 From: 3442853561 <3442853561@qq.com> Date: Tue, 23 Aug 2016 14:14:15 +0800 Subject: [PATCH 2/8] 16.8.23.14.13 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加貢獻者 --- src/CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CONTRIBUTORS.md b/src/CONTRIBUTORS.md index 3087d3f..e7c2ba9 100644 --- a/src/CONTRIBUTORS.md +++ b/src/CONTRIBUTORS.md @@ -5,4 +5,5 @@ * [Askeing Yen](https://github.com/askeing) * [Keith Yeung](https://github.com/KiChjang) * [Shing Lyu](https://github.com/shinglyu) +* [Yevgeny Liu](https://github.com/3442853561) * [YodaLee](https://github.com/yodalee) From 51d7bd68f481979b14897ad63b481c89cd7c64da Mon Sep 17 00:00:00 2001 From: 3442853561 <3442853561@qq.com> Date: Tue, 23 Aug 2016 14:15:50 +0800 Subject: [PATCH 3/8] 16.8.23.14.15 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改目錄 --- src/SUMMARY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7962f42..65fc3e7 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -28,8 +28,8 @@ * [Trait Objects](trait-objects.md) * [Closures](closures.md) * [Universal Function Call Syntax](ufcs.md) - * [Crates and Modules](crates-and-modules.md) - * [`const` and `static`](const-and-static.md) + * [箱與模塊](crates-and-modules.md) + * [常量與靜態量](const-and-static.md) * [Attributes](attributes.md) * [`type` aliases](type-aliases.md) * [Casting between types](casting-between-types.md) From c4012e9889953f663008032a9f82863ef4d935c0 Mon Sep 17 00:00:00 2001 From: 3442853561 <3442853561@qq.com> Date: Tue, 23 Aug 2016 14:20:07 +0800 Subject: [PATCH 4/8] 16.8.23.14.19 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新英漢對照表 --- src/MappingTable.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/MappingTable.md b/src/MappingTable.md index d1c4afa..2a7016e 100644 --- a/src/MappingTable.md +++ b/src/MappingTable.md @@ -43,6 +43,7 @@ expression-oriented | 表達式導向 | expression statements | 表達陳述式 | extensions | 擴充功能 | handle | 控制代碼 | 參考:[維基百科][handle]、[MSDN][handle_2] +hash | 哈希/散列 | heap | 堆積 | 參考:[維基百科][heap] fault | 錯誤 | formalization | 正規化 | @@ -58,6 +59,8 @@ inheritance | 繼承  | language feature | 語言特徵 | 參考:[中華民國資訊學會][language feature] library | 函式庫 | 參考:[維基百科][library] lifetimes | 生命週期 | +linker | 連接器 | +linking | 連接 | loop | 迴圈、循環 | 參考:[維基百科][loop] macro | 巨集 | 參考:[維基百科][macro] main function | 主函式 | 參考:[維基百科][main function] @@ -81,6 +84,7 @@ pointer | 指標 | 參考:[維基百 polymorphism | 多型 | 參考:[維基百科][polymorphism] primitive type | 基本型別 | 參考:[維基百科][primitive type] reference | 參照、參考 | 參考:[維基百科][reference] +root | 根 | round bracket | 圓括號 | 參考:[維基百科][bracket] scope | 有效範圍 | semantics | 語意 | @@ -88,6 +92,7 @@ segment | 區段 | 參考:[維基百 segmentation fault | 記憶體區段錯誤 | 參考:[維基百科][segmentation fault] shadowing | 遮蔽 | signed integer | 帶號整數 | 參考:[維基百科][integer] +shell | shell命令 | square bracket | 方括號 | 參考:[維基百科][bracket] stack | 堆疊 | 參考:[維基百科][stack] statements | 陳述式 | @@ -188,10 +193,7 @@ crates | | dependencies | | destructuring let | | 用於存取 tuple fully-strict | | -hash | | import | | -linker | | -linking | | master | | git branch mata | | metaprogramming | | @@ -203,8 +205,6 @@ prelude | | 預先載入的函式 profiles | | regression | | repository | | -root | | -shell | | slices | | 其他資料結構的參考 tabs | | target triple | | From 49fa6e18b9d67563e67dbd4c39ced634caed8270 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Tue, 23 Aug 2016 15:11:11 +0800 Subject: [PATCH 5/8] 16.8.23.15.10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 翻譯完成《常量與靜態量》 --- src/const-and-static.md | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 2a5dce7..8d332b2 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -6,7 +6,7 @@ const N: i32 = 5; ``` -你必須明確一個 `const` 的數據類型,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 +你必須明確一個 `const` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 [let]: variable-bindings.html @@ -14,10 +14,7 @@ const N: i32 = 5; # `static` -Rust provides a ‘global variable’ sort of facility in static items. They’re -similar to constants, but static items aren’t inlined upon use. This means that -there is only one instance for each value, and it’s at a fixed location in -memory. +在Rust語言中,“全局變量”是以靜態量的形式體現的。靜態量與常量是類似的,只不過在靜態量被使用時不發生內聯。換句話說,每一個靜態量都只有一個實體,並且位於內存中唯一確定的位置。 這裡有一道例題: @@ -25,10 +22,9 @@ memory. static N: i32 = 5; ``` -Unlike [`let`][let] bindings, you must annotate the type of a `static`. +你必須明確一個 `static` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 -Statics live for the entire lifetime of a program, and therefore any -reference stored in a constant has a [`'static` lifetime][lifetimes]: +靜態量作用於整個程序的生命週期,所以任何儲蓄在常量中的引用都有 [靜態量生命週期][lifetimes]: ```rust static NAME: &'static str = "Steve"; @@ -36,17 +32,15 @@ static NAME: &'static str = "Steve"; [lifetimes]: lifetimes.html -## Mutability +## 可變性 -You can introduce mutability with the `mut` keyword: +當您使用 `mut` 關鍵字的時候可以引入可變性: ```rust static mut N: i32 = 5; ``` -Because this is mutable, one thread could be updating `N` while another is -reading it, causing memory unsafety. As such both accessing and mutating a -`static mut` is [`unsafe`][unsafe], and so must be done in an `unsafe` block: +正因為它這種可變性導致一個線程正在修改變量`N`的時候,可能有另一個線程正在讀取它。這樣一來,內存就處於不安全的狀態。因此無論是修改一個`static mut`,還是讀取一個`static mut` 都是 [不安全的][unsafe], 所以它必須在 `unsafe` 區塊中才能操作: ```rust # static mut N: i32 = 5; @@ -60,23 +54,20 @@ unsafe { [unsafe]: unsafe.html -Furthermore, any type stored in a `static` must be `Sync`, and may not have -a [`Drop`][drop] implementation. +更進一步的說,任何儲蓄在 `static` 中的型別都必須實現 `Sync`, 而且不能實現 [`Drop`][drop]。 [drop]: drop.html -# Initializing +# 初始化 -Both `const` and `static` have requirements for giving them a value. They may -only be given a value that’s a constant expression. In other words, you cannot -use the result of a function call or anything similarly complex or at runtime. +無論 `const` 還是 `static` 都需要被賦予一個值,且只能被賦予常數表達式的值。 換句話說,您不能使用一個函數的返回值或者任何相似的復合值對它賦值,也不能在程式運行的過程中賦值。 -# Which construct should I use? +> 譯者注:賦值的行為被稱之為初始化。 -Almost always, if you can choose between the two, choose `const`. It’s pretty -rare that you actually want a memory location associated with your constant, -and using a const allows for optimizations like constant propagation not only -in your crate but downstream crates. +# 我應該選擇使用哪一種構造? +當您無所謂選擇哪個的絕大多數時候就選擇 `const`。 您只要極少數情況需要關心常量映射在內存中的地址,而且使用 `const` 允許您在自己的箱和衍生箱中像常數擴展那樣優化它。 + +> 譯者注:Rust語言中的常量相當於C語言中的#define > *commit 9eda98a* From 427286802a37fff984c8473e883815f28f8fcecd Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Wed, 24 Aug 2016 04:25:55 +0800 Subject: [PATCH 6/8] Update drop.md --- src/drop.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/drop.md b/src/drop.md index 7dd3db0..c10e0d3 100644 --- a/src/drop.md +++ b/src/drop.md @@ -48,21 +48,16 @@ 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之前離開作用域。 -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. +那麼 `Drop` 有什麼好處呢?通常情況下 `Drop` 被用來清理和 `struct` 關聯得到資源. 譬如說[`Arc` 型別][arc]是一個引用計數型別。當 `Drop` 被調用的時候,它就會減少引用計數。此外,如果引用的總數為零,底層的值將會被擦除。 [arc]: ../std/sync/struct.Arc.html From 11552ab4df4125eaef615cc794f78eeaae9c2970 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Wed, 24 Aug 2016 04:44:23 +0800 Subject: [PATCH 7/8] 16.8.24.4.44 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 翻譯完成 --- src/drop.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/drop.md b/src/drop.md index c10e0d3..ac7a03e 100644 --- a/src/drop.md +++ b/src/drop.md @@ -1,8 +1,6 @@ -% 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 +21,9 @@ 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 { From f0fa478d90c1b922c0ad0936380965e6a85ded41 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Wed, 24 Aug 2016 10:08:56 +0800 Subject: [PATCH 8/8] 16.8.24.10.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接受了 @KiChjang 的建議 --- src/const-and-static.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 8d332b2..469752e 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -6,7 +6,7 @@ const N: i32 = 5; ``` -你必須明確一個 `const` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 +你必須明確指定一個 `const` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 [let]: variable-bindings.html @@ -22,7 +22,7 @@ const N: i32 = 5; static N: i32 = 5; ``` -你必須明確一個 `static` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 +你必須明確指定一個 `static` 的型別,這和使用 [`let`][let] 關鍵字進行綁定並不相同。 靜態量作用於整個程序的生命週期,所以任何儲蓄在常量中的引用都有 [靜態量生命週期][lifetimes]: