From 3e26c0feed2273a97a422aa71e884d87d08ffe05 Mon Sep 17 00:00:00 2001 From: Martin Geisler <mgeisler@google.com> Date: Wed, 28 Jun 2023 09:15:18 +0200 Subject: [PATCH] zh-CN: translate control-flow (#866) Part of #324. --- po/zh-CN.po | 260 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 246 insertions(+), 14 deletions(-) diff --git a/po/zh-CN.po b/po/zh-CN.po index 8f84ada8..8d049b5e 100644 --- a/po/zh-CN.po +++ b/po/zh-CN.po @@ -6433,7 +6433,7 @@ msgstr "" #: src/control-flow.md:1 msgid "# Control Flow" -msgstr "" +msgstr "# 控制流" #: src/control-flow.md:3 msgid "" @@ -6444,10 +6444,14 @@ msgid "" "similarly\n" "in Rust." msgstr "" +"正如我们所知,`if` 是 Rust 中的一个表达式。它用于有条件地\n" +"评估两个块中的一个,但这些块可以有一个值,\n" +"然后成为 `if` 表达式的值。其他控制流表达式在 Rust 中也有类似\n" +"的运作方式。" #: src/control-flow/blocks.md:1 msgid "# Blocks" -msgstr "" +msgstr "# 块" #: src/control-flow/blocks.md:3 msgid "" @@ -6455,6 +6459,8 @@ msgid "" "the\n" "block:" msgstr "" +"Rust 中的块包含值和类型:值是\n" +"块的最后一个表达式:" #: src/control-flow/blocks.md:6 msgid "" @@ -6477,12 +6483,32 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let x = {\n" +" let y = 10;\n" +" println!(\"y: {y}\");\n" +" let z = {\n" +" let w = {\n" +" 3 + 4\n" +" };\n" +" println!(\"w: {w}\");\n" +" y * w\n" +" };\n" +" println!(\"z: {z}\");\n" +" z - y\n" +" };\n" +" println!(\"x: {x}\");\n" +"}\n" +"```" #: src/control-flow/blocks.md:25 msgid "" "The same rule is used for functions: the value of the function body is the\n" "return value:" msgstr "" +"同样的规则也适用于函数:函数主体的值\n" +"是返回值:" #: src/control-flow/blocks.md:28 msgid "" @@ -6496,12 +6522,21 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn double(x: i32) -> i32 {\n" +" x + x\n" +"}\n" +"\n" +"fn main() {\n" +" println!(\"doubled: {}\", double(7));\n" +"}\n" +"```" #: src/control-flow/blocks.md:38 msgid "" "However if the last expression ends with `;`, then the resulting value and " "type is `()`." -msgstr "" +msgstr "不过,如果最后一个表达式以 `;` 结尾,那么生成的值和类型为 `()`。" #: src/control-flow/blocks.md:43 msgid "" @@ -6512,10 +6547,14 @@ msgid "" "`return`.\n" " " msgstr "" +"* 这张幻灯片的重点是说明在 Rust 中,块有类型和值。\n" +"* 你可以通过更改块的最后一行,来展示块值的变化情况。例如,添加/移除分号或使" +"用 `return`。\n" +" " #: src/control-flow/if-expressions.md:1 msgid "# `if` expressions" -msgstr "" +msgstr "# `if` 表达式" #: src/control-flow/if-expressions.md:3 msgid "" @@ -6524,6 +6563,10 @@ msgid "" "expressions)\n" "exactly like `if` statements in other languages:" msgstr "" +"[`if`\n" +"表达式](https://doc.rust-lang.org/reference/expressions/if-expr.html#if-" +"expressions)\n" +"的用法与其他语言中的 `if` 语句完全一样。" #: src/control-flow/if-expressions.md:7 msgid "" @@ -6538,12 +6581,24 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let mut x = 10;\n" +" if x % 2 == 0 {\n" +" x = x / 2;\n" +" } else {\n" +" x = 3 * x + 1;\n" +" }\n" +"}\n" +"```" #: src/control-flow/if-expressions.md:18 msgid "" "In addition, you can use `if` as an expression. The last expression of each\n" "block becomes the value of the `if` expression:" msgstr "" +"此外,你还可以将 `if` 用作一个表达式。每个块的最后一个表达式\n" +"将成为 `if` 表达式的值:" #: src/control-flow/if-expressions.md:22 msgid "" @@ -6558,6 +6613,16 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let mut x = 10;\n" +" x = if x % 2 == 0 {\n" +" x / 2\n" +" } else {\n" +" 3 * x + 1\n" +" };\n" +"}\n" +"```" #: src/control-flow/if-expressions.md:35 msgid "" @@ -6565,10 +6630,12 @@ msgid "" "branch blocks must have the same type. Consider showing what happens if you " "add `;` after `x / 2` in the second example." msgstr "" +"由于 `if` 是一个表达式且必须有一个特定的类型,因此它的两个分支块必须有相同的" +"类型。考虑在第二个示例中将 `;` 添加到 `x / 2` 的后面,看看会出现什么情况。" #: src/control-flow/if-let-expressions.md:1 msgid "# `if let` expressions" -msgstr "" +msgstr "# `if let` 表达式" #: src/control-flow/if-let-expressions.md:3 msgid "" @@ -6578,6 +6645,10 @@ msgid "" "lets you execute different code depending on whether a value matches a " "pattern:" msgstr "" +"[`if let`\n" +"表达式](https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-" +"expressions)\n" +"能让你根据某个值是否与模式相匹配来执行不同的代码:" #: src/control-flow/if-let-expressions.md:7 msgid "" @@ -6592,6 +6663,16 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let arg = std::env::args().next();\n" +" if let Some(value) = arg {\n" +" println!(\"Program name: {value}\");\n" +" } else {\n" +" println!(\"Missing name?\");\n" +" }\n" +"}\n" +"```" #: src/control-flow/if-let-expressions.md:18 #: src/control-flow/while-let-expressions.md:21 @@ -6601,6 +6682,8 @@ msgid "" "in\n" "Rust." msgstr "" +"如需详细了解 Rust 中\n" +"的模式,请参阅[模式匹配](../pattern-matching.md)。" #: src/control-flow/if-let-expressions.md:23 msgid "" @@ -6627,10 +6710,30 @@ msgid "" " Some(item.to_uppercase())\n" " }" msgstr "" +"* `if let` 可能比 `match` 更简洁,例如,当只关注一种情况时。相比之下," +"`match` 要求覆盖所有分支。\n" +"* 使用 `Option` 时,常见的做法是处理 `Some` 值。\n" +"* 与 `match` 不同的是,`if let` 不支持模式匹配的 guard 子句。\n" +"* 自 1.65 版以来,类似的 [let-else](https://doc.rust-lang.org/rust-by-" +"example/flow_control/let_else.html) 结构允许执行解构赋值,或者如果不满足条" +"件,则有一个非返回块分支 (panic/return/break/continue):\n" +"\n" +" ```rust,editable\n" +" fn main() {\n" +" println!(\"{:?}\", second_word_to_upper(\"foo bar\"));\n" +" }\n" +" \n" +" fn second_word_to_upper(s: &str) -> Option<String> {\n" +" let mut it = s.split(' ');\n" +" let (Some(_), Some(item)) = (it.next(), it.next()) else {\n" +" return None;\n" +" };\n" +" Some(item.to_uppercase())\n" +" }" #: src/control-flow/while-expressions.md:1 msgid "# `while` loops" -msgstr "" +msgstr "# `while` 循环" #: src/control-flow/while-expressions.md:3 msgid "" @@ -6638,6 +6741,9 @@ msgid "" "expr.html#predicate-loops)\n" "works very similar to other languages:" msgstr "" +"[`while` 关键字](https://doc.rust-lang.org/reference/expressions/loop-expr." +"html#predicate-loops)\n" +" 的工作方式与其他语言非常相似:" #: src/control-flow/while-expressions.md:6 msgid "" @@ -6655,10 +6761,23 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let mut x = 10;\n" +" while x != 1 {\n" +" x = if x % 2 == 0 {\n" +" x / 2\n" +" } else {\n" +" 3 * x + 1\n" +" };\n" +" }\n" +" println!(\"Final x: {x}\");\n" +"}\n" +"```" #: src/control-flow/while-let-expressions.md:1 msgid "# `while let` loops" -msgstr "" +msgstr "# `while let` 循环" #: src/control-flow/while-let-expressions.md:3 msgid "" @@ -6666,6 +6785,9 @@ msgid "" "reference/expressions/loop-expr.html#predicate-pattern-loops)\n" "variant which repeatedly tests a value against a pattern:" msgstr "" +"与 `if let` 一样,[`with let`](https://doc.rust-lang.org/reference/" +"expressions/loop-expr.html#predicate-pattern-loops)\n" +"变体会针对一个模式重复测试一个值:" #: src/control-flow/while-let-expressions.md:6 msgid "" @@ -6680,6 +6802,16 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let v = vec![10, 20, 30];\n" +" let mut iter = v.into_iter();\n" +"\n" +" while let Some(x) = iter.next() {\n" +" println!(\"x: {x}\");\n" +" }\n" +"}\n" +"```" #: src/control-flow/while-let-expressions.md:17 msgid "" @@ -6689,6 +6821,10 @@ msgid "" "will\n" "return `None`. The `while let` lets us keep iterating through all items." msgstr "" +"在这里,每次\n" +"调用 `next()` 时,`v.iter()` 返回的迭代器都会返回一个 `Option<i32>`。它将一直" +"返回 `Some(x)`,直到完成。\n" +" 之后它将返回 `None`。`while let`能让我们持续迭代所有项。" #: src/control-flow/while-let-expressions.md:26 msgid "" @@ -6699,10 +6835,14 @@ msgid "" "The `while let` provides syntactic sugar for the above scenario.\n" " " msgstr "" +"* 指出只要值与模式匹配,`while let` 循环就会一直进行下去。\n" +"* 你可以使用 if 语句将 `while let` 循环重写为无限循环,当 `iter.next()` 没有" +"值可以解封时中断。`while let` 为上述情况提供了语法糖。\n" +" " #: src/control-flow/for-expressions.md:1 msgid "# `for` loops" -msgstr "" +msgstr "# `for` 循环" #: src/control-flow/for-expressions.md:3 msgid "" @@ -6710,6 +6850,9 @@ msgid "" "related to the [`while let` loop](while-let-expression.md). It will\n" "automatically call `into_iter()` on the expression and then iterate over it:" msgstr "" +"[`for` 循环](https://doc.rust-lang.org/std/keyword.for.html)\n" +"与 [`when let` 循环](when-let-expression.md)密切相关。它会\n" +" 自动对表达式调用 `into_iter()`,然后对其进行迭代:" #: src/control-flow/for-expressions.md:7 msgid "" @@ -6727,10 +6870,23 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let v = vec![10, 20, 30];\n" +"\n" +" for x in v {\n" +" println!(\"x: {x}\");\n" +" }\n" +" \n" +" for i in (0..10).step_by(2) {\n" +" println!(\"i: {i}\");\n" +" }\n" +"}\n" +"```" #: src/control-flow/for-expressions.md:21 msgid "You can use `break` and `continue` here as usual." -msgstr "" +msgstr "你可以在此照常使用 `break` 和 `continue`。" #: src/control-flow/for-expressions.md:25 msgid "" @@ -6741,10 +6897,15 @@ msgid "" "* Modify the elements in the vector and explain the compiler errors. Change " "vector `v` to be mutable and the for loop to `for x in v.iter_mut()`." msgstr "" +"* 在这种情况下,索引迭代在 Rust 中并不是一个特殊的语法。\n" +"* `(0..10)` 是实现 `Iterator` trait 的范围。\n" +"* `step_by` 是返回另一个 `Iterator` 的方法,用于逐一跳过所有其他元素。\n" +"* 修改矢量中的元素并说明编译器错误。将矢量 `v` 改为可变,并将 for 循环改为 " +"`for x in v.iter_mut()`。" #: src/control-flow/loop-expressions.md:1 msgid "# `loop` expressions" -msgstr "" +msgstr "# `loop` 表达式" #: src/control-flow/loop-expressions.md:3 msgid "" @@ -6752,10 +6913,13 @@ msgid "" "expressions/loop-expr.html#infinite-loops)\n" "which creates an endless loop." msgstr "" +"最后是用于创建无限循环的 [`loop` 关键字](https://doc.rust-lang.org/reference/" +"expressions/loop-expr.html#infinite-loops)\n" +"。" #: src/control-flow/loop-expressions.md:6 msgid "Here you must either `break` or `return` to stop the loop:" -msgstr "" +msgstr "在下例中,你必须 `break` 或 `return` 才能停止循环:" #: src/control-flow/loop-expressions.md:8 msgid "" @@ -6776,6 +6940,22 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let mut x = 10;\n" +" loop {\n" +" x = if x % 2 == 0 {\n" +" x / 2\n" +" } else {\n" +" 3 * x + 1\n" +" };\n" +" if x == 1 {\n" +" break;\n" +" }\n" +" }\n" +" println!(\"Final x: {x}\");\n" +"}\n" +"```" #: src/control-flow/loop-expressions.md:27 msgid "" @@ -6786,10 +6966,13 @@ msgid "" "(unlike\n" " `while` and `for` loops)." msgstr "" +"* 用一个值(例如 `break 8`)来中断 `loop` 并将其输出。\n" +"* 请注意,`loop` 是唯一返回有意义的值的循环结构。\n" +" 这是因为它保证至少被输入一次(与 `while` 和 `for` 循环不同)。" #: src/control-flow/match-expressions.md:1 msgid "# `match` expressions" -msgstr "" +msgstr "# `match` 表达式" #: src/control-flow/match-expressions.md:3 msgid "" @@ -6799,6 +6982,10 @@ msgid "" "works\n" "like a series of `if let` expressions:" msgstr "" +"[`match` 关键字](https://doc.rust-lang.org/reference/expressions/match-expr." +"html)\n" +"用于将一个值与一个或多个模式进行匹配。从这个意义上讲,它的工作方式\n" +"类似于一系列的 `if let` 表达式:" #: src/control-flow/match-expressions.md:7 msgid "" @@ -6815,12 +7002,26 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" match std::env::args().next().as_deref() {\n" +" Some(\"cat\") => println!(\"Will do cat things\"),\n" +" Some(\"ls\") => println!(\"Will ls some files\"),\n" +" Some(\"mv\") => println!(\"Let's move some files\"),\n" +" Some(\"rm\") => println!(\"Uh, dangerous!\"),\n" +" None => println!(\"Hmm, no program name?\"),\n" +" _ => println!(\"Unknown program name!\"),\n" +" }\n" +"}\n" +"```" #: src/control-flow/match-expressions.md:20 msgid "" "Like `if let`, each match arm must have the same type. The type is the last\n" "expression of the block, if any. In the example above, the type is `()`." msgstr "" +"与 `if let` 类似,每个匹配分支必须有相同的类型。该类型是块的最后一个\n" +"表达式(如有)。在上例中,类型是 `()`。" #: src/control-flow/match-expressions.md:28 msgid "" @@ -6833,10 +7034,17 @@ msgid "" " * We can now use pattern matching to match against the `&str` inside " "`Option`." msgstr "" +"* 将 match 表达式保存到一个变量中并输出结果。\n" +"* 移除 `.as_deref()` 并说明错误。\n" +" * `std::env::args().next()` 会返回 `Option<String>`,但无法与 `String` " +"进行匹配。\n" +" * `as_deref()` 会将 `Option<T>` 转换为 `Option<&T::Target>`。在我们的示例" +"中,这会将 `Option<String>` 转换为 `Option<&str>`。\n" +" * 现在,我们可以使用模式匹配来匹配 `Option` 中的 `&str`。" #: src/control-flow/break-continue.md:1 msgid "# `break` and `continue`" -msgstr "" +msgstr "# `break` 和 `continue`" #: src/control-flow/break-continue.md:3 msgid "" @@ -6846,6 +7054,11 @@ msgid "" "the next iteration use [`continue`](https://doc.rust-lang.org/reference/" "expressions/loop-expr.html#continue-expressions)." msgstr "" +"- 如果你想提前退出循环,请使用 [`break`](https://doc.rust-lang.org/reference/" +"expressions/loop-expr.html#break-expressions),\n" +"- 如果需要立即启动\n" +"下一次迭代,请使用 [`continue`](https://doc.rust-lang.org/reference/" +"expressions/loop-expr.html#continue-expressions)。" #: src/control-flow/break-continue.md:7 msgid "" @@ -6853,6 +7066,8 @@ msgid "" "used\n" "to break out of nested loops:" msgstr "" +"`continue` 和 `break` 都可以选择接受一个标签参数,用来\n" +"终止嵌套循环:" #: src/control-flow/break-continue.md:10 msgid "" @@ -6874,11 +7089,28 @@ msgid "" "}\n" "```" msgstr "" +"```rust,editable\n" +"fn main() {\n" +" let v = vec![10, 20, 30];\n" +" let mut iter = v.into_iter();\n" +" 'outer: while let Some(x) = iter.next() {\n" +" println!(\"x: {x}\");\n" +" let mut i = 0;\n" +" while i < x {\n" +" println!(\"x: {x}, i: {i}\");\n" +" i += 1;\n" +" if i == 3 {\n" +" break 'outer;\n" +" }\n" +" }\n" +" }\n" +"}\n" +"```" #: src/control-flow/break-continue.md:28 msgid "" "In this case we break the outer loop after 3 iterations of the inner loop." -msgstr "" +msgstr "在本示例中,我们会在内循环 3 次迭代后终止外循环。" #: src/std.md:1 msgid "# Standard Library"