From 6fb0d477eee3a555383b0879d3baa9f1a785490e Mon Sep 17 00:00:00 2001 From: wangmengc Date: Wed, 27 Mar 2024 16:01:40 +0800 Subject: [PATCH 1/8] print.md synchronizes official documents --- english/src/hello/print.md | 91 +++++++++++++++++++++++--------------- src/hello/print.md | 62 +++++++++++++++++--------- 2 files changed, 98 insertions(+), 55 deletions(-) diff --git a/english/src/hello/print.md b/english/src/hello/print.md index 89d2aa0d..abc26d4b 100644 --- a/english/src/hello/print.md +++ b/english/src/hello/print.md @@ -1,13 +1,15 @@ # Formatted print -Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt] -some of which include: +Printing is handled by a series of [`macros`][macros] defined in +[`std::fmt`][fmt] some of which include: * `format!`: write formatted text to [`String`][string] -* `print!`: same as `format!` but the text is printed to the console (io::stdout). +* `print!`: same as `format!` but the text is printed to the console + (io::stdout). * `println!`: same as `print!` but a newline is appended. -* `eprint!`: same as `format!` but the text is printed to the standard error (io::stderr). -* `eprintln!`: same as `eprint!`but a newline is appended. +* `eprint!`: same as `print!` but the text is printed to the standard error + (io::stderr). +* `eprintln!`: same as `eprint!` but a newline is appended. All parse text in the same fashion. As a plus, Rust checks formatting correctness at compile time. @@ -18,11 +20,9 @@ fn main() { // arguments. These will be stringified. println!("{} days", 31); - // Without a suffix, 31 becomes an i32. You can change what type 31 is - // by providing a suffix. The number 31i64 for example has the type i64. - - // There are various optional patterns this works with. Positional - // arguments can be used. + // Positional arguments can be used. Specifying an integer inside `{}` + // determines which additional argument will be replaced. Arguments start + // at 0 immediately after the format string. println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"); // As can named arguments. @@ -31,29 +31,46 @@ fn main() { subject="the quick brown fox", verb="jumps over"); - // Special formatting can be specified after a `:`. - println!("{} of {:b} people know binary, the other half doesn't", 1, 2); + // Different formatting can be invoked by specifying the format character + // after a `:`. + println!("Base 10: {}", 69420); // 69420 + println!("Base 2 (binary): {:b}", 69420); // 10000111100101100 + println!("Base 8 (octal): {:o}", 69420); // 207454 + println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c + + // You can right-justify text with a specified width. This will + // output " 1". (Four white spaces and a "1", for a total width of 5.) + println!("{number:>5}", number=1); - // You can right-align text with a specified width. This will output - // " 1". 5 white spaces and a "1". - println!("{number:>width$}", number=1, width=6); + // You can pad numbers with extra zeroes, + println!("{number:0>5}", number=1); // 00001 + // and left-adjust by flipping the sign. This will output "10000". + println!("{number:0<5}", number=1); // 10000 - // You can pad numbers with extra zeroes. This will output "000001". - println!("{number:0>width$}", number=1, width=6); + // You can use named arguments in the format specifier by appending a `$`. + println!("{number:0>width$}", number=1, width=5); - // Rust even checks to make sure the correct number of arguments are - // used. + // Rust even checks to make sure the correct number of arguments are used. println!("My name is {0}, {1} {0}", "Bond"); // FIXME ^ Add the missing argument: "James" - // Create a structure named `Structure` which contains an `i32`. - #[allow(dead_code)] + // Only types that implement fmt::Display can be formatted with `{}`. User- + // defined types do not implement fmt::Display by default. + + #[allow(dead_code)] // disable `dead_code` which warn against unused module struct Structure(i32); - // However, custom types such as this structure require more complicated - // handling. This will not work. - println!("This struct `{}` won't print...", Structure(3)); - // FIXME ^ Comment out this line. + // This will not compile because `Structure` does not implement + // fmt::Display. + // println!("This struct `{}` won't print...", Structure(3)); + // TODO ^ Try uncommenting this line + + // For Rust 1.58 and above, you can directly capture the argument from a + // surrounding variable. Just like the above, this will output + // " 1", 4 white spaces and a "1". + let number: f64 = 1.0; + let width: usize = 5; + println!("{number:>width$}"); } ``` @@ -62,7 +79,7 @@ of text. The base form of two important ones are listed below: * `fmt::Debug`: Uses the `{:?}` marker. Format text for debugging purposes. * `fmt::Display`: Uses the `{}` marker. Format text in a more elegant, user -friendly fashion. + friendly fashion. Here, we used `fmt::Display` because the std library provides implementations for these types. To print text for custom types, more steps are required. @@ -70,20 +87,22 @@ for these types. To print text for custom types, more steps are required. Implementing the `fmt::Display` trait automatically implements the [`ToString`] trait which allows us to [convert] the type to [`String`][string]. +In *line 43*, `#[allow(dead_code)]` is an [attribute] which only applies to the module after it. + ### Activities - * Fix the two issues in the above code (see FIXME) so that it runs without - error. - * Add a `println!` macro that prints: `Pi is roughly 3.142` by controlling - the number of decimal places shown. For the purposes of this exercise, - use `let pi = 3.141592` as an estimate for pi. (Hint: you may need to - check the [`std::fmt`][fmt] documentation for setting the number of - decimals to display) +* Fix the issue in the above code (see FIXME) so that it runs without + error. +* Try uncommenting the line that attempts to format the `Structure` struct + (see TODO) +* Add a `println!` macro call that prints: `Pi is roughly 3.142` by controlling + the number of decimal places shown. For the purposes of this exercise, use + `let pi = 3.141592` as an estimate for pi. (Hint: you may need to check the + [`std::fmt`][fmt] documentation for setting the number of decimals to display) ### See also: -[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], -and [`traits`][traits] +[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], [`traits`][traits], and [`dead_code`][dead_code] [fmt]: https://doc.rust-lang.org/std/fmt/ [macros]: ../macros.md @@ -92,3 +111,5 @@ and [`traits`][traits] [traits]: https://doc.rust-lang.org/std/fmt/#formatting-traits [`ToString`]: https://doc.rust-lang.org/std/string/trait.ToString.html [convert]: ../conversion/string.md +[attribute]: ../attribute.md +[dead_code]: ../attribute/unused.md diff --git a/src/hello/print.md b/src/hello/print.md index d02ab02e..5aa5301a 100644 --- a/src/hello/print.md +++ b/src/hello/print.md @@ -16,11 +16,9 @@ fn main() { // 变量内容会转化成字符串。 println!("{} days", 31); - // 不加后缀的话,31 就自动成为 i32 类型。 - // 你可以添加后缀来改变 31 的类型(例如使用 31i64 声明 31 为 i64 类型)。 - - // 用变量替换字符串有多种写法。 - // 比如可以使用位置参数。 + // 可以使用位置参数。 + // 在“{}”中指定一个整数确定将替换哪个附加参数。 + // 参数从紧接在格式字符串之后的0开始。 println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"); // 可以使用命名参数。 @@ -29,28 +27,43 @@ fn main() { subject="the quick brown fox", verb="jumps over"); - // 可以在 `:` 后面指定特殊的格式。 - println!("{} of {:b} people know binary, the other half don't", 1, 2); + // 通过在':'后面指定格式字符,可以调用不同的格式。 + println!("Base 10: {}", 69420); // 69420 + println!("Base 2 (binary): {:b}", 69420); // 10000111100101100 + println!("Base 8 (octal): {:o}", 69420); // 207454 + println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c + + // 可以用指定的宽度对文本进行右对齐。 + // 这将输出“ 1”。(四个空格和一个“1”,总宽度为5) + println!("{number:>5}", number=1); - // 你可以按指定宽度来右对齐文本。 - // 下面语句输出 " 1",5 个空格后面连着 1。 - println!("{number:>width$}", number=1, width=6); + // 你可以在数字左边补 0, + println!("{number:0>5}", number=1); // 00001 + // 在数字右边补零,下面语句输出 "000001"。 + println!("{number:0<5}", number=1); // 10000 - // 你可以在数字左边补 0。下面语句输出 "000001"。 - println!("{number:>0width$}", number=1, width=6); + // 可以在格式说明符中添加'$'来使用命名参数。 + println!("{number:0>width$}", number=1, width=5); // println! 会检查使用到的参数数量是否正确。 println!("My name is {0}, {1} {0}", "Bond"); - // 改正 ^ 补上漏掉的参数:"James" + // FIXME ^ 补上漏掉的参数:"James" - // 创建一个包含单个 `i32` 的结构体(structure)。命名为 `Structure`。 - #[allow(dead_code)] + // 只有实现了fmt::Display的类型才能使用'{}'进行格式化。 + // 默认情况下,用户定义的类型不实现fmt::Display。 + #[allow(dead_code)] // 禁用对未使用模块发出警告的' dead_code ' struct Structure(i32); - // 但是像结构体这样的自定义类型需要更复杂的方式来处理。 + // 这将不会编译,因为'结构'没有实现 // 下面语句无法运行。 - println!("This struct `{}` won't print...", Structure(3)); - // 改正 ^ 注释掉此行。 + // println!("This struct `{}` won't print...", Structure(3)); + // TODO ^ 注释掉此行。 + + // 对于Rust 1.58及更高版本,您可以直接从周围变量捕获参数。 + // 就像上面一样,这将输出“ 1”,4个空格和一个“1”。 + let number: f64 = 1.0; + let width: usize = 5; + println!("{number:>width$}"); } ``` @@ -61,17 +74,26 @@ fn main() { 上例使用了 `fmt::Display`,因为标准库提供了那些类型的实现。若要打印自定义类型的文本,需要更多的步骤。 +实现`fmt::Display`特性会自动实现[`ToString`]特征,它允许我们将类型[convert]为[`String`][String]。 + +在*第43行*中,`#[allow(dead_code)]`是一个[attribute],只适用于它后面的模块。 + ### 动手试一试 -- 改正上面代码中的两个错误(见 “改正”),使它可以没有错误地运行。 +- 改正上面代码中的错误(见 “FIXME”),使它可以没有错误地运行。 +- 尝试取消注释,输出结构体内容(见“TODO”)。 - 再用一个 `println!` 宏,通过控制显示的小数位数来打印:`Pi is roughly 3.142`(Pi 约等于 3.142)。为了达到练习目的,使用 `let pi = 3.141592` 作为 Pi 的近似值(提示:设置小数位的显示格式可以参考文档 [`std::fmt`][fmt])。 ### 参见: -[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs] 和 [`trait`][traits] +[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], [`trait`][traits] 和 [`dead_code`][dead_code] [fmt]: https://rustwiki.org/zh-CN/std/fmt/ [macros]: ../macros.md [string]: ../std/str.md [structs]: ../custom_types/structs.md [traits]: ../trait.md +[`ToString`]: https://doc.rust-lang.org/std/string/trait.ToString.html +[convert]: ../conversion/string.md +[attribute]: ../attribute.md +[dead_code]: ../attribute/unused.md From 5df0c5626a2429deccb9efb707a4f9aebb970ee9 Mon Sep 17 00:00:00 2001 From: wangmengc Date: Mon, 15 Apr 2024 13:59:27 +0800 Subject: [PATCH 2/8] Modified format without translation --- src/hello/print.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/hello/print.md b/src/hello/print.md index 5aa5301a..192b33c2 100644 --- a/src/hello/print.md +++ b/src/hello/print.md @@ -17,8 +17,8 @@ fn main() { println!("{} days", 31); // 可以使用位置参数。 - // 在“{}”中指定一个整数确定将替换哪个附加参数。 - // 参数从紧接在格式字符串之后的0开始。 + // 在 `{}` 中指定一个整数确定将替换哪个附加参数。 + // 参数从紧接在格式字符串之后的 0 开始。 println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"); // 可以使用命名参数。 @@ -27,7 +27,7 @@ fn main() { subject="the quick brown fox", verb="jumps over"); - // 通过在':'后面指定格式字符,可以调用不同的格式。 + // 通过在 `:` 后面指定格式字符,可以调用不同的格式。 println!("Base 10: {}", 69420); // 69420 println!("Base 2 (binary): {:b}", 69420); // 10000111100101100 println!("Base 8 (octal): {:o}", 69420); // 207454 @@ -42,7 +42,7 @@ fn main() { // 在数字右边补零,下面语句输出 "000001"。 println!("{number:0<5}", number=1); // 10000 - // 可以在格式说明符中添加'$'来使用命名参数。 + // 可以通过添加 `$` 在格式说明符中使用命名参数。 println!("{number:0>width$}", number=1, width=5); // println! 会检查使用到的参数数量是否正确。 @@ -54,39 +54,39 @@ fn main() { #[allow(dead_code)] // 禁用对未使用模块发出警告的' dead_code ' struct Structure(i32); - // 这将不会编译,因为'结构'没有实现 + // 这将不会编译,因为`Structure`没有实现 // 下面语句无法运行。 // println!("This struct `{}` won't print...", Structure(3)); // TODO ^ 注释掉此行。 - // 对于Rust 1.58及更高版本,您可以直接从周围变量捕获参数。 - // 就像上面一样,这将输出“ 1”,4个空格和一个“1”。 + // 对于 Rust 1.58 及更高版本,您可以直接从周围变量捕获参数。 + // 就像上面一样,这将输出“ 1”,4 个空格和 1 个“1”。 let number: f64 = 1.0; let width: usize = 5; println!("{number:>width$}"); } ``` -[`std::fmt`][fmt] 包含多种 [`trait`][traits](特质)来控制文字显示,其中重要的两种 trait 的基本形式如下: +[`std::fmt`][fmt] 包含多种 [`traits`][traits](特质)来控制文字显示,其中重要的两种 trait 的基本形式如下: - `fmt::Debug`:使用 `{:?}` 标记。格式化文本以供调试使用。 - `fmt::Display`:使用 `{}` 标记。以更优雅和友好的风格来格式化文本。 上例使用了 `fmt::Display`,因为标准库提供了那些类型的实现。若要打印自定义类型的文本,需要更多的步骤。 -实现`fmt::Display`特性会自动实现[`ToString`]特征,它允许我们将类型[convert]为[`String`][String]。 +实现 `fmt::Display` 特性会自动实现 [`ToString`] 特征,它允许我们将类型 [convert] 为 [`String`][string]。 -在*第43行*中,`#[allow(dead_code)]`是一个[attribute],只适用于它后面的模块。 +在*第 43 行*中,`#[allow(dead_code)]` 是一个 [attribute],只适用于它后面的模块。 ### 动手试一试 -- 改正上面代码中的错误(见 “FIXME”),使它可以没有错误地运行。 -- 尝试取消注释,输出结构体内容(见“TODO”)。 +- 改正上面代码中的错误(见 FIXME),使它可以没有错误地运行。 +- 尝试取消注释,输出结构体内容(见 TODO)。 - 再用一个 `println!` 宏,通过控制显示的小数位数来打印:`Pi is roughly 3.142`(Pi 约等于 3.142)。为了达到练习目的,使用 `let pi = 3.141592` 作为 Pi 的近似值(提示:设置小数位的显示格式可以参考文档 [`std::fmt`][fmt])。 ### 参见: -[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], [`trait`][traits] 和 [`dead_code`][dead_code] +[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], [`traits`][traits] 和 [`dead_code`][dead_code] [fmt]: https://rustwiki.org/zh-CN/std/fmt/ [macros]: ../macros.md From 050c153630a33a480623cee8a5af4e84b355ba93 Mon Sep 17 00:00:00 2001 From: YangFong Date: Mon, 15 Apr 2024 14:03:49 +0800 Subject: [PATCH 3/8] Update print.md --- src/hello/print.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hello/print.md b/src/hello/print.md index 192b33c2..528008e9 100644 --- a/src/hello/print.md +++ b/src/hello/print.md @@ -54,7 +54,7 @@ fn main() { #[allow(dead_code)] // 禁用对未使用模块发出警告的' dead_code ' struct Structure(i32); - // 这将不会编译,因为`Structure`没有实现 + // 这将不会编译,因为 `Structure` 没有实现 // 下面语句无法运行。 // println!("This struct `{}` won't print...", Structure(3)); // TODO ^ 注释掉此行。 From bc766c1d104f24fc5fb9afb26402a1ac6f408626 Mon Sep 17 00:00:00 2001 From: YangFong Date: Mon, 15 Apr 2024 14:06:19 +0800 Subject: [PATCH 4/8] Update src/hello/print.md --- src/hello/print.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hello/print.md b/src/hello/print.md index 528008e9..5734a694 100644 --- a/src/hello/print.md +++ b/src/hello/print.md @@ -59,7 +59,7 @@ fn main() { // println!("This struct `{}` won't print...", Structure(3)); // TODO ^ 注释掉此行。 - // 对于 Rust 1.58 及更高版本,您可以直接从周围变量捕获参数。 + // 对于 Rust 1.58 及更高版本,你可以直接从周围变量捕获参数。 // 就像上面一样,这将输出“ 1”,4 个空格和 1 个“1”。 let number: f64 = 1.0; let width: usize = 5; From 96bd4f98a782a418573d2b0249dcd916938d8ff1 Mon Sep 17 00:00:00 2001 From: wangmengc Date: Tue, 16 Apr 2024 17:18:57 +0800 Subject: [PATCH 5/8] update trait.md --- english/src/trait.md | 6 +++--- src/trait.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/english/src/trait.md b/english/src/trait.md index 7878d474..d4d40946 100644 --- a/english/src/trait.md +++ b/english/src/trait.md @@ -12,10 +12,10 @@ methods from `Animal` with a `Sheep`. struct Sheep { naked: bool, name: &'static str } trait Animal { - // Static method signature; `Self` refers to the implementor type. + // Associated function signature; `Self` refers to the implementor type. fn new(name: &'static str) -> Self; - // Instance method signatures; these will return a string. + // Method signatures; these will return a string. fn name(&self) -> &'static str; fn noise(&self) -> &'static str; @@ -77,4 +77,4 @@ fn main() { dolly.shear(); dolly.talk(); } -``` \ No newline at end of file +``` diff --git a/src/trait.md b/src/trait.md index 76f63d5d..7a424c0e 100644 --- a/src/trait.md +++ b/src/trait.md @@ -8,7 +8,7 @@ struct Sheep { naked: bool, name: &'static str } trait Animal { - // 静态方法签名;`Self` 表示实现者类型(implementor type)。 + // 关联函数签名;`Self` 表示实现者类型(implementor type)。 fn new(name: &'static str) -> Self; // 实例方法签名;这些方法将返回一个字符串。 From 177e8862d4b781a6f02527adece70f9128aab7ae Mon Sep 17 00:00:00 2001 From: wangmengc Date: Tue, 30 Apr 2024 13:46:55 +0800 Subject: [PATCH 6/8] update flow_control/match/destructuring/destructure_slice.md --- .../match/destructuring/destructure_slice.md | 48 +++++++++++++++++++ .../match/destructuring/destructure_slice.md | 45 +++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 english/src/flow_control/match/destructuring/destructure_slice.md create mode 100644 src/flow_control/match/destructuring/destructure_slice.md diff --git a/english/src/flow_control/match/destructuring/destructure_slice.md b/english/src/flow_control/match/destructuring/destructure_slice.md new file mode 100644 index 00000000..93b7e420 --- /dev/null +++ b/english/src/flow_control/match/destructuring/destructure_slice.md @@ -0,0 +1,48 @@ +# arrays/slices + +Like tuples, arrays and slices can be destructured this way: + +```rust,editable +fn main() { + // Try changing the values in the array, or make it a slice! + let array = [1, -2, 6]; + + match array { + // Binds the second and the third elements to the respective variables + [0, second, third] => + println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third), + + // Single values can be ignored with _ + [1, _, third] => println!( + "array[0] = 1, array[2] = {} and array[1] was ignored", + third + ), + + // You can also bind some and ignore the rest + [-1, second, ..] => println!( + "array[0] = -1, array[1] = {} and all the other ones were ignored", + second + ), + // The code below would not compile + // [-1, second] => ... + + // Or store them in another array/slice (the type depends on + // that of the value that is being matched against) + [3, second, tail @ ..] => println!( + "array[0] = 3, array[1] = {} and the other elements were {:?}", + second, tail + ), + + // Combining these patterns, we can, for example, bind the first and + // last values, and store the rest of them in a single array + [first, middle @ .., last] => println!( + "array[0] = {}, middle = {:?}, array[2] = {}", + first, middle, last + ), + } +} +``` + +### See also: + +[Arrays and Slices](../../../primitives/array.md) and [Binding](../binding.md) for `@` sigil diff --git a/src/flow_control/match/destructuring/destructure_slice.md b/src/flow_control/match/destructuring/destructure_slice.md new file mode 100644 index 00000000..ffe443cd --- /dev/null +++ b/src/flow_control/match/destructuring/destructure_slice.md @@ -0,0 +1,45 @@ +# 数组/切片 + +像元组一样,数组和切片也可以这样解构: + +```rust,editable +fn main() { + // 尝试改变数组中的值,或者将其做成切片! + let array = [1, -2, 6]; + + match array { + // 将第二个和第三个元素绑定到各自的变量 + [0, second, third] => + println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third), + + // 单个值可以用 ‘_’ 忽略 + [1, _, third] => println!( + "array[0] = 1, array[2] = {} and array[1] was ignored", + third + ), + + // 也可以绑定一些而忽略其余的 + [-1, second, ..] => println!( + "array[0] = -1, array[1] = {} and all the other ones were ignored", + second + ), + // 下面的代码无法编译 + // [-1, second] => ... + + // 或者将它们存储在另一个数组/切片中(类型取决于所匹配的值的类型)。 + [3, second, tail @ ..] => println!( + "array[0] = 3, array[1] = {} and the other elements were {:?}", + second, tail + ), + + // 结合这些模式,我们可以绑定第一个和最后一个值,并将其余的值存储在单个数组中 + [first, middle @ .., last] => println!( + "array[0] = {}, middle = {:?}, array[2] = {}", + first, middle, last + ), + } +} +``` + +### 参见: +[数组和切片](../../../primitives/array.md) 和 `@`符号用法[绑定](../binding.md) \ No newline at end of file From 1b81853217da950889aa01cc45d5ad28e11dad63 Mon Sep 17 00:00:00 2001 From: wangmengc Date: Wed, 8 May 2024 09:34:46 +0800 Subject: [PATCH 7/8] Modify as suggested --- .../match/destructuring/destructure_slice.md | 10 +++++----- src/trait.md | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/flow_control/match/destructuring/destructure_slice.md b/src/flow_control/match/destructuring/destructure_slice.md index ffe443cd..ce8431f2 100644 --- a/src/flow_control/match/destructuring/destructure_slice.md +++ b/src/flow_control/match/destructuring/destructure_slice.md @@ -12,13 +12,13 @@ fn main() { [0, second, third] => println!("array[0] = 0, array[1] = {}, array[2] = {}", second, third), - // 单个值可以用 ‘_’ 忽略 + // 单个值可以用 `_` 忽略 [1, _, third] => println!( "array[0] = 1, array[2] = {} and array[1] was ignored", third ), - // 也可以绑定一些而忽略其余的 + // 你也可以绑定一些而忽略其余的 [-1, second, ..] => println!( "array[0] = -1, array[1] = {} and all the other ones were ignored", second @@ -26,13 +26,13 @@ fn main() { // 下面的代码无法编译 // [-1, second] => ... - // 或者将它们存储在另一个数组/切片中(类型取决于所匹配的值的类型)。 + // 或者将它们存储在另一个数组/切片中(类型取决于所匹配的值的类型) [3, second, tail @ ..] => println!( "array[0] = 3, array[1] = {} and the other elements were {:?}", second, tail ), - // 结合这些模式,我们可以绑定第一个和最后一个值,并将其余的值存储在单个数组中 + // 结合这些模式,我们可以绑定第一个和最后一个值,并将其余的值存储在一个数组中 [first, middle @ .., last] => println!( "array[0] = {}, middle = {:?}, array[2] = {}", first, middle, last @@ -42,4 +42,4 @@ fn main() { ``` ### 参见: -[数组和切片](../../../primitives/array.md) 和 `@`符号用法[绑定](../binding.md) \ No newline at end of file +[数组和切片](../../../primitives/array.md) 与 `@` 符号用法[绑定](../binding.md) \ No newline at end of file diff --git a/src/trait.md b/src/trait.md index 7a424c0e..db3f3802 100644 --- a/src/trait.md +++ b/src/trait.md @@ -11,7 +11,7 @@ trait Animal { // 关联函数签名;`Self` 表示实现者类型(implementor type)。 fn new(name: &'static str) -> Self; - // 实例方法签名;这些方法将返回一个字符串。 + // 方法签名;这些方法将返回一个字符串。 fn name(&self) -> &'static str; fn noise(&self) -> &'static str; From 136f69f17b158e0c1e32f3a154dfb1e9f3636ae5 Mon Sep 17 00:00:00 2001 From: YangFong Date: Wed, 8 May 2024 14:05:59 +0800 Subject: [PATCH 8/8] Update SUMMARY.md --- src/SUMMARY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index bfbb450d..ff9a05a4 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -52,6 +52,7 @@ - [match 匹配](flow_control/match.md) - [解构](flow_control/match/destructuring.md) - [元组](flow_control/match/destructuring/destructure_tuple.md) + - [数组/切片](flow_control/match/destructuring/destructure_slice.md) - [枚举](flow_control/match/destructuring/destructure_enum.md) - [指针和引用](flow_control/match/destructuring/destructure_pointers.md) - [结构体](flow_control/match/destructuring/destructure_structures.md)