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 | 哈希/散列 |
Copy link
Owner

Choose a reason for hiding this comment

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

此處在台灣一般保留原文 hash 或稱為 雜湊 (故 Hash Table 有人稱為 雜湊表)。
Ref: https://zh.wikipedia.org/zh-tw/%E6%95%A3%E5%88%97

Copy link
Author

Choose a reason for hiding this comment

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

我淚流滿面的表示自己在北京是打不開維基百科的

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命令 |
Copy link
Owner

@askeing askeing Aug 24, 2016

Choose a reason for hiding this comment

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

Shell命令 似乎有過度翻譯的情形,容易被誤會為 Shell Command 的翻譯。
建議保留原文即可。

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)
Copy link
Owner

@askeing askeing Aug 24, 2016

Choose a reason for hiding this comment

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

Crates 是 Rust 的特殊用法,建議保留原文。
另外 Modules 依照對照表應翻為 模組

Copy link
Author

Choose a reason for hiding this comment

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

在一些簡體版本的譯本中已經把Rust語言中的Crate翻譯為包裝箱了。所以要不要採納這種譯法?

Copy link
Author

Choose a reason for hiding this comment

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

如果把crate翻譯為箱的話,那麼downstream crate就可以順理成章的翻譯為衍生箱

* [常量與靜態量](const-and-static.md)
Copy link
Owner

Choose a reason for hiding this comment

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

Static 一般翻為 靜態變數
另外 static 靜態變數 也可以加入對照表中。
Ref: https://zh.wikipedia.org/zh-tw/%E9%9D%99%E6%80%81%E5%8F%98%E9%87%8F

* [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`
% 常量與靜態量
Copy link
Owner

Choose a reason for hiding this comment

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

同上,建議翻為靜態變數


Rust has a way of defining constants with the `const` keyword:
在Rust語言中定義常量可以使用 `const` 關鍵字:
Copy link
Owner

Choose a reason for hiding this comment

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

建議把 常量 依照對照表翻譯為 常數


```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語言中,“全局變量”是以靜態量的形式體現的。靜態量與常量是類似的,只不過在靜態量被使用時不發生內聯。換句話說,每一個靜態量都只有一個實體,並且位於內存中唯一確定的位置。
Copy link
Owner

Choose a reason for hiding this comment

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

一般我們會把 global variable 稱為 全域變數


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` 區塊中才能操作:
Copy link
Owner

Choose a reason for hiding this comment

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

thread 建議使用 執行緒


```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]。
Copy link
Owner

Choose a reason for hiding this comment

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

儲存


[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` 允許您在自己的箱和衍生箱中像常數擴展那樣優化它。
Copy link
Owner

Choose a reason for hiding this comment

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

只有極少數的情況下你需要關心常數在記憶體中的位址,而且使用 const 可以在 crate 和 downstream crate 中使用像是常數傳播的技術最佳化。

建議新增 constant propagation 常數傳播 到對照表
Ref: https://zh.wikipedia.org/zh-tw/%E5%B8%B8%E6%95%B8%E6%8A%98%E7%96%8A

Copy link
Author

Choose a reason for hiding this comment

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

同樣,在北京是沒有辦法打開維基百科的網站的


> 譯者注:Rust語言中的常量相當於C語言中的#define

> *commit 9eda98a*
25 changes: 7 additions & 18 deletions src/drop.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 {
Expand All @@ -48,21 +42,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<T>` 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<T>` 型別][arc]是一個引用計數型別。當 `Drop` 被調用的時候,它就會減少引用計數。此外,如果引用的總數為零,底層的值將會被擦除。

[arc]: ../std/sync/struct.Arc.html

Expand Down