mirror of
https://github.com/google/comprehensive-rust.git
synced 2024-12-15 06:20:32 +02:00
zh-TW: translate ownership (#860)
* zh-TW: translate ownership Part of #684. * Apply suggestions from code review Co-authored-by: Jonathan Hao <phao@chromium.org> * Replace "擁有權" with "所有權" * Remove "__" --------- Co-authored-by: Jonathan Hao <phao@chromium.org> Co-authored-by: Jonathan Hao <phao@google.com>
This commit is contained in:
parent
cb8acfe2da
commit
b14aaf768d
371
po/zh-TW.po
371
po/zh-TW.po
@ -3750,14 +3750,14 @@ msgstr ""
|
||||
|
||||
#: src/ownership.md:1
|
||||
msgid "# Ownership"
|
||||
msgstr ""
|
||||
msgstr "# 所有權"
|
||||
|
||||
#: src/ownership.md:3
|
||||
msgid ""
|
||||
"All variable bindings have a _scope_ where they are valid and it is an error "
|
||||
"to\n"
|
||||
"use a variable outside its scope:"
|
||||
msgstr ""
|
||||
msgstr "所有變數繫結都會在特定「範圍」內有效,在範圍外使用變數會是錯誤:"
|
||||
|
||||
#: src/ownership.md:6
|
||||
msgid ""
|
||||
@ -3773,6 +3773,17 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable,compile_fail\n"
|
||||
"struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" {\n"
|
||||
" let p = Point(3, 4);\n"
|
||||
" println!(\"x: {}\", p.0);\n"
|
||||
" }\n"
|
||||
" println!(\"y: {}\", p.1);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership.md:18
|
||||
msgid ""
|
||||
@ -3780,14 +3791,17 @@ msgid ""
|
||||
"* A destructor can run here to free up resources.\n"
|
||||
"* We say that the variable _owns_ the value."
|
||||
msgstr ""
|
||||
"* 範圍結束時,變數會遭到「捨棄」,資料也會釋放。\n"
|
||||
"* 解構函式可在這時執行,用來釋放資源。\n"
|
||||
"* 我們會說變數「擁有」值。"
|
||||
|
||||
#: src/ownership/move-semantics.md:1
|
||||
msgid "# Move Semantics"
|
||||
msgstr ""
|
||||
msgstr "# 移動語意"
|
||||
|
||||
#: src/ownership/move-semantics.md:3
|
||||
msgid "An assignment will transfer ownership between variables:"
|
||||
msgstr ""
|
||||
msgstr "指派會在變數之間轉移所有權:"
|
||||
|
||||
#: src/ownership/move-semantics.md:5
|
||||
msgid ""
|
||||
@ -3800,6 +3814,14 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let s1: String = String::from(\"Hello!\");\n"
|
||||
" let s2: String = s1;\n"
|
||||
" println!(\"s2: {s2}\");\n"
|
||||
" // println!(\"s1: {s1}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/move-semantics.md:14
|
||||
msgid ""
|
||||
@ -3809,6 +3831,11 @@ msgid ""
|
||||
"* When `s2` goes out of scope, the string data is freed.\n"
|
||||
"* There is always _exactly_ one variable binding which owns a value."
|
||||
msgstr ""
|
||||
"* 將 `s1` 指派給 `s2` 會轉移所有權。\n"
|
||||
"* 資料已從 `s1`「移出」,`s1` 無法再供存取。\n"
|
||||
"* 當 `s1` 超出範圍時,系統不會執行任何動作,因為 `s1` 沒有所有權。\n"
|
||||
"* 當 `s2` 超出範圍時,系統會釋放字串資料。\n"
|
||||
"* 一律「只有」一個變數綁定會擁有值。"
|
||||
|
||||
#: src/ownership/move-semantics.md:22
|
||||
msgid ""
|
||||
@ -3817,10 +3844,14 @@ msgid ""
|
||||
"\n"
|
||||
"* In Rust, clones are explicit (by using `clone`)."
|
||||
msgstr ""
|
||||
"* 請說明這與 C++ 中的預設情形相反:您必須使用 `std::move`,且已定義移動建構函"
|
||||
"式,系統才會根據值進行複製。\n"
|
||||
"\n"
|
||||
"* 在 Rust 中,克隆作業皆為明確設定,方法為使用 `clone`。"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:1
|
||||
msgid "# Moved Strings in Rust"
|
||||
msgstr ""
|
||||
msgstr "# Rust 中移動的字串"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:3
|
||||
msgid ""
|
||||
@ -3831,16 +3862,24 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let s1: String = String::from(\"Rust\");\n"
|
||||
" let s2: String = s1;\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:10
|
||||
msgid ""
|
||||
"* The heap data from `s1` is reused for `s2`.\n"
|
||||
"* When `s1` goes out of scope, nothing happens (it has been moved from)."
|
||||
msgstr ""
|
||||
"* 系統會為 `s2` 重複使用 `s1` 的堆積資料。\n"
|
||||
"* 當 `s1` 超出範圍時,系統不會執行任何動作,因為 `s1` 已移出。"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:13
|
||||
msgid "Before move to `s2`:"
|
||||
msgstr ""
|
||||
msgstr "移至 `s2` 前:"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:15
|
||||
msgid ""
|
||||
@ -3859,10 +3898,24 @@ msgid ""
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```bob\n"
|
||||
" 堆疊 堆積\n"
|
||||
".- - - - - - - - - - - - - -. .- - - - - - - - - - - - - -.\n"
|
||||
": : : :\n"
|
||||
": s1 : : :\n"
|
||||
": +-----------+-------+ : : +----+----+----+----+ :\n"
|
||||
": | ptr | o---+---+-----+-->| R | u | s | t | :\n"
|
||||
": | len | 4 | : : +----+----+----+----+ :\n"
|
||||
": | capacity | 4 | : : :\n"
|
||||
": +-----------+-------+ : : :\n"
|
||||
": : `- - - - - - - - - - - - - -'\n"
|
||||
": :\n"
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:30
|
||||
msgid "After move to `s2`:"
|
||||
msgstr ""
|
||||
msgstr "移至 `s2` 後:"
|
||||
|
||||
#: src/ownership/moved-strings-rust.md:32
|
||||
msgid ""
|
||||
@ -3887,14 +3940,34 @@ msgid ""
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```bob\n"
|
||||
" 堆疊 堆積\n"
|
||||
".- - - - - - - - - - - - - -. .- - - - - - - - - - - - - -.\n"
|
||||
": : : :\n"
|
||||
": s1 \"(inaccessible)\" : : :\n"
|
||||
": +-----------+-------+ : : +----+----+----+----+ :\n"
|
||||
": | ptr | o---+---+--+--+-->| R | u | s | t | :\n"
|
||||
": | len | 4 | : | : +----+----+----+----+ :\n"
|
||||
": | capacity | 4 | : | : :\n"
|
||||
": +-----------+-------+ : | : :\n"
|
||||
": : | `- - - - - - - - - - - - - -'\n"
|
||||
": s2 : |\n"
|
||||
": +-----------+-------+ : |\n"
|
||||
": | ptr | o---+---+--'\n"
|
||||
": | len | 4 | :\n"
|
||||
": | capacity | 4 | :\n"
|
||||
": +-----------+-------+ :\n"
|
||||
": :\n"
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:1
|
||||
msgid "# Double Frees in Modern C++"
|
||||
msgstr ""
|
||||
msgstr "# 現代 C++ 中的雙重釋放"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:3
|
||||
msgid "Modern C++ solves this differently:"
|
||||
msgstr ""
|
||||
msgstr "現代 C++ 可使用不同方式解決這個問題:"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:5
|
||||
msgid ""
|
||||
@ -3903,6 +3976,10 @@ msgid ""
|
||||
"std::string s2 = s1; // Duplicate the data in s1.\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```c++\n"
|
||||
"std::string s1 = \"Cpp\";\n"
|
||||
"std::string s2 = s1; // 重複 s1 中的資料。\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:10
|
||||
msgid ""
|
||||
@ -3910,10 +3987,12 @@ msgid ""
|
||||
"copy.\n"
|
||||
"* When `s1` and `s2` go out of scope, they each free their own memory."
|
||||
msgstr ""
|
||||
"* `s1` 的堆積資料會重複,`s2` 會取得專屬的獨立副本。\n"
|
||||
"* 當 `s1` 和 `s2` 超出範圍時,皆會釋放自己的記憶體。"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:13
|
||||
msgid "Before copy-assignment:"
|
||||
msgstr ""
|
||||
msgstr "複製指派前:"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:16
|
||||
msgid ""
|
||||
@ -3931,10 +4010,23 @@ msgid ""
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```bob\n"
|
||||
" 堆疊 堆積\n"
|
||||
".- - - - - - - - - - - - - -. .- - - - - - - - - - - -.\n"
|
||||
": : : :\n"
|
||||
": s1 : : :\n"
|
||||
": +-----------+-------+ : : +----+----+----+ :\n"
|
||||
": | ptr | o---+---+--+--+-->| C | p | p | :\n"
|
||||
": | len | 3 | : : +----+----+----+ :\n"
|
||||
": | capacity | 3 | : : :\n"
|
||||
": +-----------+-------+ : : :\n"
|
||||
": : `- - - - - - - - - - - -'\n"
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:30
|
||||
msgid "After copy-assignment:"
|
||||
msgstr ""
|
||||
msgstr "複製指派後:"
|
||||
|
||||
#: src/ownership/double-free-modern-cpp.md:32
|
||||
msgid ""
|
||||
@ -3959,16 +4051,36 @@ msgid ""
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```bob\n"
|
||||
" 堆疊 堆積\n"
|
||||
".- - - - - - - - - - - - - -. .- - - - - - - - - - - -.\n"
|
||||
": : : :\n"
|
||||
": s1 : : :\n"
|
||||
": +-----------+-------+ : : +----+----+----+ :\n"
|
||||
": | ptr | o---+---+--+--+-->| C | p | p | :\n"
|
||||
": | len | 3 | : : +----+----+----+ :\n"
|
||||
": | capacity | 3 | : : :\n"
|
||||
": +-----------+-------+ : : :\n"
|
||||
": : : :\n"
|
||||
": s2 : : :\n"
|
||||
": +-----------+-------+ : : +----+----+----+ :\n"
|
||||
": | ptr | o---+---+-----+-->| C | p | p | :\n"
|
||||
": | len | 3 | : : +----+----+----+ :\n"
|
||||
": | capacity | 3 | : : :\n"
|
||||
": +-----------+-------+ : : :\n"
|
||||
": : `- - - - - - - - - - - -'\n"
|
||||
"`- - - - - - - - - - - - - -'\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/moves-function-calls.md:1
|
||||
msgid "# Moves in Function Calls"
|
||||
msgstr ""
|
||||
msgstr "# 函式呼叫中的移動"
|
||||
|
||||
#: src/ownership/moves-function-calls.md:3
|
||||
msgid ""
|
||||
"When you pass a value to a function, the value is assigned to the function\n"
|
||||
"parameter. This transfers ownership:"
|
||||
msgstr ""
|
||||
msgstr "將值傳遞至函式時,該值會指派給函式參數。這麼做會轉移所有權:"
|
||||
|
||||
#: src/ownership/moves-function-calls.md:6
|
||||
msgid ""
|
||||
@ -3984,6 +4096,17 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn say_hello(name: String) {\n"
|
||||
" println!(\"Hello {name}\")\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let name = String::from(\"Alice\");\n"
|
||||
" say_hello(name);\n"
|
||||
" // say_hello(name);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/moves-function-calls.md:20
|
||||
msgid ""
|
||||
@ -3999,15 +4122,23 @@ msgid ""
|
||||
"move semantics the default, and by forcing programmers to make clones "
|
||||
"explicit."
|
||||
msgstr ""
|
||||
"* 首次呼叫 `say_hello` 時,`main` 會放棄 `name` 的所有權。之後,`name` 就無法"
|
||||
"在 `main` 內使用。\n"
|
||||
"* 為 `name` 分配的堆積記憶體會在 `say_hello` 函式結束時釋放。\n"
|
||||
"* 如果 `main` 以參照的形式傳送 `name` (`&name`),且 `say_hello` 能以參數的形"
|
||||
"式接受參照,`main` 就可以保留所有權。\n"
|
||||
"* 另外,`main` 可在首次呼叫 (`name.clone()`) 中傳遞 `name` 的克隆。\n"
|
||||
"* 在 Rust 中,移動語意為預設做法,且強制規定程式設計師必須明確設定克隆,因此"
|
||||
"不小心建立副本的可能性就會低於在 C++ 中。"
|
||||
|
||||
#: src/ownership/copy-clone.md:1
|
||||
msgid "# Copying and Cloning"
|
||||
msgstr ""
|
||||
msgstr "# 複製和克隆"
|
||||
|
||||
#: src/ownership/copy-clone.md:3
|
||||
msgid ""
|
||||
"While move semantics are the default, certain types are copied by default:"
|
||||
msgstr ""
|
||||
msgstr "雖然移動語意是預設做法,但某些類型的預設做法為複製:"
|
||||
|
||||
#: src/ownership/copy-clone.md:5
|
||||
msgid ""
|
||||
@ -4020,14 +4151,22 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let x = 42;\n"
|
||||
" let y = x;\n"
|
||||
" println!(\"x: {x}\");\n"
|
||||
" println!(\"y: {y}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/copy-clone.md:14
|
||||
msgid "These types implement the `Copy` trait."
|
||||
msgstr ""
|
||||
msgstr "這些類型會實作 `Copy` 特徵。"
|
||||
|
||||
#: src/ownership/copy-clone.md:16
|
||||
msgid "You can opt-in your own types to use copy semantics:"
|
||||
msgstr ""
|
||||
msgstr "您可以自行選擇加入類型,使用複製語意的做法:"
|
||||
|
||||
#: src/ownership/copy-clone.md:18
|
||||
msgid ""
|
||||
@ -4043,16 +4182,29 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Copy, Clone, Debug)]\n"
|
||||
"struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let p1 = Point(3, 4);\n"
|
||||
" let p2 = p1;\n"
|
||||
" println!(\"p1: {p1:?}\");\n"
|
||||
" println!(\"p2: {p2:?}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/copy-clone.md:30
|
||||
msgid ""
|
||||
"* After the assignment, both `p1` and `p2` own their own data.\n"
|
||||
"* We can also use `p1.clone()` to explicitly copy the data."
|
||||
msgstr ""
|
||||
"* 指派後,`p1` 和 `p2` 都會擁有自己的資料。\n"
|
||||
"* 我們也能使用 `p1.clone()` 明確複製資料。"
|
||||
|
||||
#: src/ownership/copy-clone.md:35
|
||||
msgid "Copying and cloning are not the same thing:"
|
||||
msgstr ""
|
||||
msgstr "複製和克隆並不相同:"
|
||||
|
||||
#: src/ownership/copy-clone.md:37
|
||||
msgid ""
|
||||
@ -4064,10 +4216,14 @@ msgid ""
|
||||
"implementing the `Clone` trait.\n"
|
||||
"* Copying does not work on types that implement the `Drop` trait."
|
||||
msgstr ""
|
||||
"* 複製是指記憶體區域的按位元複製作業,不適用於任意物件。\n"
|
||||
"* 複製不允許用於自訂邏輯,這與 C++ 中的複製建構函式不同。\n"
|
||||
"* 克隆是較廣泛的作業,而且只要實作 `Clone` 特徵,即允許用於自訂行為。\n"
|
||||
"* 複製不適用於實作 `Drop` 特徵的類型。"
|
||||
|
||||
#: src/ownership/copy-clone.md:42 src/ownership/lifetimes-function-calls.md:29
|
||||
msgid "In the above example, try the following:"
|
||||
msgstr ""
|
||||
msgstr "在上述範例中,請嘗試下列操作:"
|
||||
|
||||
#: src/ownership/copy-clone.md:44
|
||||
msgid ""
|
||||
@ -4077,6 +4233,10 @@ msgid ""
|
||||
"the `println!` for `p1`.\n"
|
||||
"* Show that it works if you clone `p1` instead."
|
||||
msgstr ""
|
||||
"* 將 `String` 欄位新增至 `struct Point`。由於 `String` 不屬於 `Copy` 類型,因"
|
||||
"此不會編譯。\n"
|
||||
"* 從 `derive` 屬性中移除 `Copy`。編譯器錯誤現在位於 `p1` 的 `println!` 中。\n"
|
||||
"* 示範如果改為克隆 `p1`,就能正常運作。"
|
||||
|
||||
#: src/ownership/copy-clone.md:48
|
||||
msgid ""
|
||||
@ -4085,16 +4245,18 @@ msgid ""
|
||||
"at compile time. In this case the default implementations of `Copy` and "
|
||||
"`Clone` traits are generated."
|
||||
msgstr ""
|
||||
"如有學員問起 `derive`,只需回答這是在 Rust 編譯時間中產生程式碼的方式。在這種"
|
||||
"情形下,系統會產生 `Copy` 和 `Clone` 特徵的預設實作方式。"
|
||||
|
||||
#: src/ownership/borrowing.md:1
|
||||
msgid "# Borrowing"
|
||||
msgstr ""
|
||||
msgstr "# 借用"
|
||||
|
||||
#: src/ownership/borrowing.md:3
|
||||
msgid ""
|
||||
"Instead of transferring ownership when calling a function, you can let a\n"
|
||||
"function _borrow_ the value:"
|
||||
msgstr ""
|
||||
msgstr "您可以不必在呼叫函式時轉移所有權,而是讓函式「借用」值:"
|
||||
|
||||
#: src/ownership/borrowing.md:6
|
||||
msgid ""
|
||||
@ -4114,16 +4276,33 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug)]\n"
|
||||
"struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
"fn add(p1: &Point, p2: &Point) -> Point {\n"
|
||||
" Point(p1.0 + p2.0, p1.1 + p2.1)\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let p1 = Point(3, 4);\n"
|
||||
" let p2 = Point(10, 20);\n"
|
||||
" let p3 = add(&p1, &p2);\n"
|
||||
" println!(\"{p1:?} + {p2:?} = {p3:?}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/borrowing.md:22
|
||||
msgid ""
|
||||
"* The `add` function _borrows_ two points and returns a new point.\n"
|
||||
"* The caller retains ownership of the inputs."
|
||||
msgstr ""
|
||||
"* `add` 函式會「借用」兩個點,並傳回新的點。\n"
|
||||
"* 呼叫端會保留輸入內容的所有權。"
|
||||
|
||||
#: src/ownership/borrowing.md:27
|
||||
msgid "Notes on stack returns:"
|
||||
msgstr ""
|
||||
msgstr "有關堆疊回傳的注意事項:"
|
||||
|
||||
#: src/ownership/borrowing.md:28
|
||||
msgid ""
|
||||
@ -4157,20 +4336,48 @@ msgid ""
|
||||
"all. If RVO did not happen, Rust will always performs a simple and efficient "
|
||||
"`memcpy` copy."
|
||||
msgstr ""
|
||||
"* 示範因為編譯器可刪除複製操作,所以 `add` 回傳的成本較低。變更上述程式碼"
|
||||
"來顯示堆疊位址,並在 [Playground] 上執行。在「DEBUG」最佳化等級中,位址應變"
|
||||
"更,但在變更為「RELEASE」設定時,位址應維持不變:\n"
|
||||
"\n"
|
||||
" ```rust,editable\n"
|
||||
" #[derive(Debug)]\n"
|
||||
" struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
" fn add(p1: &Point, p2: &Point) -> Point {\n"
|
||||
" let p = Point(p1.0 + p2.0, p1.1 + p2.1);\n"
|
||||
" println!(\"&p.0: {:p}\", &p.0);\n"
|
||||
" p\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" fn main() {\n"
|
||||
" let p1 = Point(3, 4);\n"
|
||||
" let p2 = Point(10, 20);\n"
|
||||
" let p3 = add(&p1, &p2);\n"
|
||||
" println!(\"&p3.0: {:p}\", &p3.0);\n"
|
||||
" println!(\"{p1:?} + {p2:?} = {p3:?}\");\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
"* Rust 編譯器可以執行回傳值最佳化 (RVO)。\n"
|
||||
"* 在 C++ 中,複製省略必須定義在語言規格內,因為建構函式可能有連帶效果。在 "
|
||||
"Rust 中,這完全不是問題。如未發生 RVO,Rust 一律會執行簡單有效的 `memcpy` 複"
|
||||
"製作業。"
|
||||
|
||||
#: src/ownership/shared-unique-borrows.md:1
|
||||
msgid "# Shared and Unique Borrows"
|
||||
msgstr ""
|
||||
msgstr "# 共用借用和專屬借用"
|
||||
|
||||
#: src/ownership/shared-unique-borrows.md:3
|
||||
msgid "Rust puts constraints on the ways you can borrow values:"
|
||||
msgstr ""
|
||||
msgstr "Rust 會限制借用值的方式:"
|
||||
|
||||
#: src/ownership/shared-unique-borrows.md:5
|
||||
msgid ""
|
||||
"* You can have one or more `&T` values at any given time, _or_\n"
|
||||
"* You can have exactly one `&mut T` value."
|
||||
msgstr ""
|
||||
"* 隨時擁有一或多個 `&T` 值,「或是」\n"
|
||||
"* 只擁有一個 `&mut T` 值。"
|
||||
|
||||
#: src/ownership/shared-unique-borrows.md:8
|
||||
msgid ""
|
||||
@ -4189,6 +4396,20 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable,compile_fail\n"
|
||||
"fn main() {\n"
|
||||
" let mut a: i32 = 10;\n"
|
||||
" let b: &i32 = &a;\n"
|
||||
"\n"
|
||||
" {\n"
|
||||
" let c: &mut i32 = &mut a;\n"
|
||||
" *c = 20;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" println!(\"a: {a}\");\n"
|
||||
" println!(\"b: {b}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/shared-unique-borrows.md:25
|
||||
msgid ""
|
||||
@ -4200,14 +4421,20 @@ msgid ""
|
||||
"the new mutable borrow of `a` through `c`. This is a feature of the borrow "
|
||||
"checker called \"non-lexical lifetimes\"."
|
||||
msgstr ""
|
||||
"* 上述程式碼不會編譯,因為系統會同時透過 `c` 和 `b`,以可變動項和不可變動項的"
|
||||
"格式借用 `a`。\n"
|
||||
"* 請將 `b` 的 `println!` 陳述式移到導入 `c` 的範圍前,即可編譯程式碼。\n"
|
||||
"* 經過該變更後,編譯器會發現系統使用 `b` 的時間,只會在新可變動項透過 `c` 借"
|
||||
"用 `a` 之前。這是借用檢查器中的功能,稱為「非詞彙生命週期」(non-lexical "
|
||||
"lifetimes)。"
|
||||
|
||||
#: src/ownership/lifetimes.md:1
|
||||
msgid "# Lifetimes"
|
||||
msgstr ""
|
||||
msgstr "# 生命週期"
|
||||
|
||||
#: src/ownership/lifetimes.md:3
|
||||
msgid "A borrowed value has a _lifetime_:"
|
||||
msgstr ""
|
||||
msgstr "借用的值具有「生命週期」:"
|
||||
|
||||
#: src/ownership/lifetimes.md:5
|
||||
msgid ""
|
||||
@ -4226,16 +4453,24 @@ msgid ""
|
||||
" but Rust allows these to be elided in most cases with [a few simple\n"
|
||||
" rules](https://doc.rust-lang.org/nomicon/lifetime-elision.html)."
|
||||
msgstr ""
|
||||
"* 生命週期可以採用隱含方式:`add(p1: &Point, p2: &Point) -> Point`。\n"
|
||||
"* 生命週期也可以採用明確方式:`&'a Point`、`&'document str`。\n"
|
||||
"* 請將 `&'a Point` 讀做「至少對生命週期 `a` 有效的借用 `Point`」。\n"
|
||||
"* 生命週期一律會由編譯器推論:您無法自行指派生命週期。\n"
|
||||
" * 生命週期註解會建立限制;編譯器會驗證是否有有效的解決方案。\n"
|
||||
"* 請務必完整指定函式引數和回傳值的生命週期,不過在大部分情況下,Rust 允許使"
|
||||
"用 [一些簡單規則](https://doc.rust-lang.org/nomicon/lifetime-elision.html) 省"
|
||||
"略這些生命週期。"
|
||||
|
||||
#: src/ownership/lifetimes-function-calls.md:1
|
||||
msgid "# Lifetimes in Function Calls"
|
||||
msgstr ""
|
||||
msgstr "# 函式呼叫中的生命週期"
|
||||
|
||||
#: src/ownership/lifetimes-function-calls.md:3
|
||||
msgid ""
|
||||
"In addition to borrowing its arguments, a function can return a borrowed "
|
||||
"value:"
|
||||
msgstr ""
|
||||
msgstr "除了借用引數,函式也可以傳回借用的值:"
|
||||
|
||||
#: src/ownership/lifetimes-function-calls.md:5
|
||||
msgid ""
|
||||
@ -4255,6 +4490,21 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug)]\n"
|
||||
"struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
"fn left_most<'a>(p1: &'a Point, p2: &'a Point) -> &'a Point {\n"
|
||||
" if p1.0 < p2.0 { p1 } else { p2 }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let p1: Point = Point(10, 10);\n"
|
||||
" let p2: Point = Point(20, 20);\n"
|
||||
" let p3: &Point = left_most(&p1, &p2);\n"
|
||||
" println!(\"left-most point: {:?}\", p3);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/lifetimes-function-calls.md:21
|
||||
msgid ""
|
||||
@ -4264,6 +4514,10 @@ msgid ""
|
||||
" lifetime `a`\".\n"
|
||||
" * The _at least_ part is important when parameters are in different scopes."
|
||||
msgstr ""
|
||||
"* `'a` 是由編譯器推論的泛型參數。\n"
|
||||
"* 生命週期的開頭為 `'`,一般預設名稱為 `'a`。\n"
|
||||
"* 請將 `&'a Point` 讀做「至少對生命週期 `a` 有效的借用 `Point`」。\n"
|
||||
" * 如果參數位於不同的範圍,「至少」一詞就至關重要。"
|
||||
|
||||
#: src/ownership/lifetimes-function-calls.md:31
|
||||
msgid ""
|
||||
@ -4302,15 +4556,44 @@ msgid ""
|
||||
"returned reference is not used\n"
|
||||
" for longer than a variable from where the reference came from."
|
||||
msgstr ""
|
||||
"* 將 `p2` 和 `p3` 的宣告移至新範圍 (`{ ... }`),會產生以下程式碼:\n"
|
||||
" ```rust,ignore\n"
|
||||
" #[derive(Debug)]\n"
|
||||
" struct Point(i32, i32);\n"
|
||||
"\n"
|
||||
" fn left_most<'a>(p1: &'a Point, p2: &'a Point) -> &'a Point {\n"
|
||||
" if p1.0 < p2.0 { p1 } else { p2 }\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" fn main() {\n"
|
||||
" let p1: Point = Point(10, 10);\n"
|
||||
" let p3: &Point;\n"
|
||||
" {\n"
|
||||
" let p2: Point = Point(20, 20);\n"
|
||||
" p3 = left_most(&p1, &p2);\n"
|
||||
" }\n"
|
||||
" println!(\"left-most point: {:?}\", p3);\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
" 請注意,這在 `p3` 超越 `p2` 並繼續留存後,就沒有編譯。\n"
|
||||
"\n"
|
||||
"* 重設工作區,並將函式簽章變更為 `fn left_most<'a, 'b>(p1: &'a Point, p2: "
|
||||
"&'a Point) -> &'b Point`。這不會編譯,因為生命週期 `'a` 和 `'b` 之間的關係不"
|
||||
"明確。\n"
|
||||
"* 另一種說明方式:\n"
|
||||
" * 函式會借用兩個值的兩個參照,而函式會傳回另一個參照。\n"
|
||||
" * 該參照必須來自這兩種輸入來源的其中之一 (或來自全域變數)。\n"
|
||||
" * 究竟是哪一個來源?編譯器需要知道來源為何,因此在呼叫點上,所傳回參照的使"
|
||||
"用時間不會長於來自參照來源的變數。"
|
||||
|
||||
#: src/ownership/lifetimes-data-structures.md:1
|
||||
msgid "# Lifetimes in Data Structures"
|
||||
msgstr ""
|
||||
msgstr "# 資料結構中的生命週期"
|
||||
|
||||
#: src/ownership/lifetimes-data-structures.md:3
|
||||
msgid ""
|
||||
"If a data type stores borrowed data, it must be annotated with a lifetime:"
|
||||
msgstr ""
|
||||
msgstr "如果資料類型會儲存借用的資料,則必須使用生命週期註解:"
|
||||
|
||||
#: src/ownership/lifetimes-data-structures.md:5
|
||||
msgid ""
|
||||
@ -4333,6 +4616,24 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug)]\n"
|
||||
"struct Highlight<'doc>(&'doc str);\n"
|
||||
"\n"
|
||||
"fn erase(text: String) {\n"
|
||||
" println!(\"Bye {text}!\");\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let text = String::from(\"The quick brown fox jumps over the lazy dog."
|
||||
"\");\n"
|
||||
" let fox = Highlight(&text[4..19]);\n"
|
||||
" let dog = Highlight(&text[35..43]);\n"
|
||||
" // erase(text);\n"
|
||||
" println!(\"{fox:?}\");\n"
|
||||
" println!(\"{dog:?}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/ownership/lifetimes-data-structures.md:25
|
||||
msgid ""
|
||||
@ -4350,6 +4651,16 @@ msgid ""
|
||||
"lifetime relationships between the references themselves, in addition to the "
|
||||
"lifetime of the struct itself. Those are very advanced use cases."
|
||||
msgstr ""
|
||||
"* 在上述範例中,`Highlight` 的註解會強制執行以下規定:若是包含在內的 `&str` "
|
||||
"的基礎資料,留存時間應至少和使用該資料的所有 `Highlight` 例項一樣長。\n"
|
||||
"* 如果在 `fox` (或 `dog`) 的生命週期結束前消耗 `text`,借用檢查器會擲回錯"
|
||||
"誤。\n"
|
||||
"* 含有借用資料的類型會強制要求使用者保留原始資料。這在建立輕量檢視畫面可能很"
|
||||
"實用,但通常也會增加使用難度。\n"
|
||||
"* 請盡可能讓資料結構直接擁有資料。\n"
|
||||
"* 某些內含多個參照的結構體可擁有多個生命週期註解。如果除了結構體的生命週期之"
|
||||
"外,還需要描述參照之間的生命週期關係,就可能有必要擁有多個生命週期註解。那些"
|
||||
"是相當進階的用途。"
|
||||
|
||||
#: src/exercises/day-1/afternoon.md:1
|
||||
msgid "# Day 1: Afternoon Exercises"
|
||||
|
Loading…
Reference in New Issue
Block a user