From 3869e1ff93b1c2b15ad2ff3f81deb882cf9ef553 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 06:23:50 +0800 Subject: [PATCH 1/8] 16.8.24.6.23 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 整理到一個單獨的分支中 --- src/const-and-static.md | 59 ++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 03f072f..9010ee5 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -1,38 +1,34 @@ -% `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` +# 靜態變數 -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語言中,“全局變量”是以靜態量的形式體現的。靜態量與常量是類似的,只不過在靜態 +量被使用時不發生內聯。換句話說,每一個靜態量都只有一個實體,並且位於內存中唯一確定 +的位置。 -Here’s an example: +這裡有一道例題: ```rust static N: i32 = 5; ``` -Unlike [`let`][let] bindings, you must annotate the type of a `static`. +你必須明確指定一個`static`的型別,這和使用[`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"; @@ -40,17 +36,17 @@ 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; @@ -64,23 +60,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? +# 我應該選擇使用哪一種構造? + +當您無所謂選擇哪個的絕大多數時候就選擇 `const` 。 您只要極少數情況需要關心 +常量映射在內存中的地址,而且使用 `const`允許您在自己的箱和衍生箱中像常數擴展那樣優化它。 -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. > *commit 9eda98a* From 50528b7488b714f9d866e3baebd81c7293b528a9 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 06:40:20 +0800 Subject: [PATCH 2/8] 16.8.25.6.39 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接受了若干翻譯建議 --- src/const-and-static.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 9010ee5..73d408b 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -16,8 +16,8 @@ const N: i32 = 5; # 靜態變數 -在Rust語言中,“全局變量”是以靜態量的形式體現的。靜態量與常量是類似的,只不過在靜態 -量被使用時不發生內聯。換句話說,每一個靜態量都只有一個實體,並且位於內存中唯一確定 +在Rust語言中,“全域變數”是以靜態變數的形式體現的。靜態變數與常數是類似的,只不過在靜態 +變數被使用時不發生內聯。換句話說,每一個靜態變數都只有一個實體,並且位於內存中唯一確定 的位置。 這裡有一道例題: @@ -28,7 +28,7 @@ static N: i32 = 5; 你必須明確指定一個`static`的型別,這和使用[`let`]關鍵字進行綁定並不相同。 -靜態量作用於整個程序的生命週期,所以任何儲蓄在常量中的引用都有[靜態量生命週期][lifetimes]: +靜態變數作用於整個程式的生命週期,所以任何儲存在常量中的引用都有[靜態變數生命週期][lifetimes]: ```rust static NAME: &'static str = "Steve"; @@ -44,7 +44,7 @@ static NAME: &'static str = "Steve"; static mut N: i32 = 5; ``` -正因為它這種可變性導致一個線程正在修改變量 `N` 的時候,可能有另一個線程正在讀取 +正因為它這種可變性導致一個執行緒正在修改變數 `N` 的時候,可能有另一個線程正在讀取 它。這樣一來,內存就處於不安全的狀態。因此無論是修改一個`static mut`,還是讀取 一個`static mut`都是不安全的, 所以它必須在 [`unsafe`][unsafe]區塊中才能操作: @@ -60,7 +60,7 @@ unsafe { [unsafe]: unsafe.html -更進一步的說,任何儲蓄在 `static` 中的型別都必須實現 `Sync`, 而且不能實現[`Drop`][drop]。 +更進一步的說,任何儲存在 `static` 中的型別都必須實現 `Sync`, 而且不能實現[丟棄][drop]。 [drop]: drop.html @@ -71,8 +71,15 @@ unsafe { # 我應該選擇使用哪一種構造? -當您無所謂選擇哪個的絕大多數時候就選擇 `const` 。 您只要極少數情況需要關心 -常量映射在內存中的地址,而且使用 `const`允許您在自己的箱和衍生箱中像常數擴展那樣優化它。 +當您無所謂選擇哪個的絕大多數時候就選擇 `const` 。 只有極少數的情況下您需要關心 +常數在記憶體中的位址,而且使用 `const`允許您在自己的crate和衍生的crate中像常數擴展那樣優化它。 + +> crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 +,可是漢語中的箱和Rust中說的crate又不完全是相同的意思。不過像Java語言中的package翻譯成包已 +經能夠被接受了。Rust語言中的crate和Java語言中的package是類似的。 + + + From 3c68b82d653776dfe3f18e3275c054fc8e747d18 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 06:41:36 +0800 Subject: [PATCH 3/8] Update const-and-static.md --- src/const-and-static.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 73d408b..3b74475 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -74,13 +74,11 @@ unsafe { 當您無所謂選擇哪個的絕大多數時候就選擇 `const` 。 只有極少數的情況下您需要關心 常數在記憶體中的位址,而且使用 `const`允許您在自己的crate和衍生的crate中像常數擴展那樣優化它。 -> crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 +> 譯者注:crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 ,可是漢語中的箱和Rust中說的crate又不完全是相同的意思。不過像Java語言中的package翻譯成包已 經能夠被接受了。Rust語言中的crate和Java語言中的package是類似的。 - - - +-------------------- > *commit 9eda98a* From 344f54df44765137e75ce7dbcd80db7810570f77 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 06:46:20 +0800 Subject: [PATCH 4/8] 16.8.25.6.46 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 d5f6ccf1c3f89ddf521c27ed77f99a6916eb70ca Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 25 Aug 2016 08:55:39 +0800 Subject: [PATCH 5/8] 16.8.25.8.55 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..54878db 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -29,7 +29,7 @@ * [Closures](closures.md) * [Universal Function Call Syntax](ufcs.md) * [Crates and Modules](crates-and-modules.md) - * [`const` and `static`](const-and-static.md) + * [常數與靜態變數](const-and-static.md) * [Attributes](attributes.md) * [`type` aliases](type-aliases.md) * [Casting between types](casting-between-types.md) From e48b64fb83594b05f8e7872567988a6e26739e37 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 1 Sep 2016 21:51:40 +0800 Subject: [PATCH 6/8] 16.9.1.21.51 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新譯者注 --- src/const-and-static.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 3b74475..6651d0c 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -74,7 +74,8 @@ unsafe { 當您無所謂選擇哪個的絕大多數時候就選擇 `const` 。 只有極少數的情況下您需要關心 常數在記憶體中的位址,而且使用 `const`允許您在自己的crate和衍生的crate中像常數擴展那樣優化它。 -> 譯者注:crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 +> 譯者注:Rust語言中的常量相當於C語言中的#define (參考:[rust-lang/rust#36039](https://github.com/rust-lang/rust/issues/36039) ) +crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 ,可是漢語中的箱和Rust中說的crate又不完全是相同的意思。不過像Java語言中的package翻譯成包已 經能夠被接受了。Rust語言中的crate和Java語言中的package是類似的。 From f6f01c21c91eb47db7169aa3c9e8ef0bfefb9115 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 1 Sep 2016 21:52:21 +0800 Subject: [PATCH 7/8] 16.9.1.21.52 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正排版的問題 --- src/const-and-static.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/const-and-static.md b/src/const-and-static.md index 6651d0c..045c30b 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -75,6 +75,7 @@ unsafe { 常數在記憶體中的位址,而且使用 `const`允許您在自己的crate和衍生的crate中像常數擴展那樣優化它。 > 譯者注:Rust語言中的常量相當於C語言中的#define (參考:[rust-lang/rust#36039](https://github.com/rust-lang/rust/issues/36039) ) + crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 ,可是漢語中的箱和Rust中說的crate又不完全是相同的意思。不過像Java語言中的package翻譯成包已 經能夠被接受了。Rust語言中的crate和Java語言中的package是類似的。 From 6b37f007fe25ad95f9267e67ae2a59034ca17073 Mon Sep 17 00:00:00 2001 From: Yevgeny <3442853561@qq.com> Date: Thu, 1 Sep 2016 21:53:21 +0800 Subject: [PATCH 8/8] 16.9.1.21.53 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 若干排版問題 --- src/const-and-static.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/const-and-static.md b/src/const-and-static.md index 045c30b..f8e5d09 100644 --- a/src/const-and-static.md +++ b/src/const-and-static.md @@ -76,11 +76,10 @@ unsafe { > 譯者注:Rust語言中的常量相當於C語言中的#define (參考:[rust-lang/rust#36039](https://github.com/rust-lang/rust/issues/36039) ) -crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 + +> crate這個詞我們還沒有決定到底要不要翻譯,一些漢譯本中把它翻譯為箱。這看起來是好的 ,可是漢語中的箱和Rust中說的crate又不完全是相同的意思。不過像Java語言中的package翻譯成包已 經能夠被接受了。Rust語言中的crate和Java語言中的package是類似的。 - --------------------- > *commit 9eda98a*