Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 5 additions & 5 deletions src/MappingTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ expression-oriented | 表達式導向 |
expression statements | 表達陳述式 |
extensions | 擴充功能 |
handle | 控制代碼 | 參考:[維基百科][handle]、[MSDN][handle_2]
hash | 哈希/散列 |
heap | 堆積 | 參考:[維基百科][heap]
fault | 錯誤 |
formalization | 正規化 |
Expand All @@ -58,6 +59,8 @@ inheritance | 繼承  |
language feature | 語言特徵 | 參考:[中華民國資訊學會][language feature]
library | 函式庫 | 參考:[維基百科][library]
lifetimes | 生命週期 |
linker | 連接器 |
linking | 連接 |
loop | 迴圈、循環 | 參考:[維基百科][loop]
macro | 巨集 | 參考:[維基百科][macro]
main function | 主函式 | 參考:[維基百科][main function]
Expand All @@ -81,13 +84,15 @@ pointer | 指標 | 參考:[維基百
polymorphism | 多型 | 參考:[維基百科][polymorphism]
primitive type | 基本型別 | 參考:[維基百科][primitive type]
reference | 參照、參考 | 參考:[維基百科][reference]
root | 根 |
round bracket | 圓括號 | 參考:[維基百科][bracket]
scope | 有效範圍 |
semantics | 語意 |
segment | 區段 | 參考:[維基百科][segment]
segmentation fault | 記憶體區段錯誤 | 參考:[維基百科][segmentation fault]
shadowing | 遮蔽 |
signed integer | 帶號整數 | 參考:[維基百科][integer]
shell | shell命令 |
square bracket | 方括號 | 參考:[維基百科][bracket]
stack | 堆疊 | 參考:[維基百科][stack]
statements | 陳述式 |
Expand Down Expand Up @@ -188,10 +193,7 @@ crates | |
dependencies | |
destructuring let | | 用於存取 tuple
fully-strict | |
hash | |
import | |
linker | |
linking | |
master | | git branch
mata | |
metaprogramming | |
Expand All @@ -203,8 +205,6 @@ prelude | | 預先載入的函式
profiles | |
regression | |
repository | |
root | |
shell | |
slices | | 其他資料結構的參考
tabs | |
target triple | |
Expand Down
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 19 additions & 32 deletions src/const-and-static.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,46 @@
% `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`][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";
```

[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;
Expand All @@ -64,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我有點質疑這個說法,因為C語言也有const的關鍵字。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在想應該怎麼類比C語言,C語言中的const應該是有明確的一個內存地址的,而Rust中的const的內存地址是不確定的。如果用C語言中的#define ,雖然#define 也沒有明確內存地址,但是它根本就沒有進入內存,而是在預處理器中被執行了。

所以呢,我覺得如果要是告訴初學者不要在Rust訪問一個常量的地址還是應該類比C語言中的#define ,感覺這部分還需要再斟酌。


> *commit 9eda98a*