Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 32 additions & 33 deletions src/const-and-static.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
% `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";
```

[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 +60,26 @@ 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.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` 。 只有極少數的情況下您需要關心
Copy link
Collaborator

Choose a reason for hiding this comment

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

我的理解比較像是 「如果能有選擇,那就優先選 const」(換句話說,除非非選 static 不可不然不要)

Copy link
Author

Choose a reason for hiding this comment

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

的確是這樣意思,但是我的想法是照顧原文中“Almost always”這個詞,所以就譯出來了絕大多數時候。

常數在記憶體中的位址,而且使用 `const`允許您在自己的crate和衍生的crate中像常數擴展那樣優化它。
Copy link
Collaborator

Choose a reason for hiding this comment

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

感覺比較像是說能夠讓編譯器有機會能夠幫你優化,但是這邊感覺好像是使用者要主動優化

Copy link
Author

Choose a reason for hiding this comment

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

最開始我給出的翻譯是:當您無所謂選擇哪個的絕大多數時候就選擇 const。 您只有極少數情況需要關心常量映射在內存中的地址,而且使用 const 允許您在自己的箱和衍生箱中像常數擴展那樣優化它。

@askeing 給出了這樣一個建議:只有極少數的情況下你需要關心常數在記憶體中的位址,而且使用 const 可以在 crate 和 downstream 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是類似的。


> *commit 9eda98a*