diff --git a/zh-CN/src/basic-types/functions.md b/zh-CN/src/basic-types/functions.md index 7015b8e..b7886c9 100644 --- a/zh-CN/src/basic-types/functions.md +++ b/zh-CN/src/basic-types/functions.md @@ -1,5 +1,5 @@ # 函数 -🌟🌟🌟 +1. 🌟🌟🌟 ```rust,editable fn main() { @@ -16,7 +16,7 @@ fn sum(x, y: i32) { ``` -🌟🌟 +2. 🌟🌟 ```rust,editable fn main() { print(); @@ -29,7 +29,7 @@ fn print() -> i32 { ``` -🌟🌟🌟 +3. 🌟🌟🌟 ```rust,editable // 用两种方法求解 diff --git a/zh-CN/src/basic-types/numbers.md b/zh-CN/src/basic-types/numbers.md index 1719668..6ef4fa9 100644 --- a/zh-CN/src/basic-types/numbers.md +++ b/zh-CN/src/basic-types/numbers.md @@ -2,7 +2,7 @@ ### 整数 -🌟 +1. 🌟 > Tips: 如果我们没有显式的给予变量一个类型,那编译器会自动帮我们推导一个类型 @@ -19,7 +19,7 @@ fn main() { } ``` -🌟 +2. 🌟 ```rust,editable // 填空 @@ -28,7 +28,7 @@ fn main() { } ``` -🌟🌟🌟 +3. 🌟🌟🌟 > Tips: 如果我们没有显式的给予变量一个类型,那编译器会自动帮我们推导一个类型 @@ -46,7 +46,7 @@ fn type_of(_: &T) -> String { } ``` -🌟🌟 +4. 🌟🌟 ```rust,editable // 填空,让代码工作 @@ -56,7 +56,7 @@ fn main() { } ``` -🌟🌟 +5. 🌟🌟 ```rust,editable // 解决代码中的错误和 `panic` @@ -67,7 +67,7 @@ fn main() { } ``` -🌟🌟 +6. 🌟🌟 ```rust,editable // 修改 `assert!` 让代码工作 @@ -79,7 +79,7 @@ fn main() { ### 浮点数 -🌟 +7. 🌟 ```rust,editable @@ -90,7 +90,7 @@ fn main() { let z = 0.01_f64; // f64 } ``` -🌟🌟 使用两种方法来让下面代码工作 +8. 🌟🌟 使用两种方法来让下面代码工作 ```rust,editable @@ -101,7 +101,7 @@ fn main() { ``` ### 序列Range -🌟🌟 两个目标: 1. 修改 `assert!` 让它工作 2. 让 `println!` 输出: 97 - 122 +9. 🌟🌟 两个目标: 1. 修改 `assert!` 让它工作 2. 让 `println!` 输出: 97 - 122 ```rust,editable fn main() { @@ -118,7 +118,7 @@ fn main() { } ``` -🌟🌟 +10. 🌟🌟 ```rust,editable // 填空 @@ -131,7 +131,7 @@ fn main() { ### 计算 -🌟 +11. 🌟 ```rust,editable // 填空,并解决错误 diff --git a/zh-CN/src/basic-types/statements-expressions.md b/zh-CN/src/basic-types/statements-expressions.md index 8a066fa..f6d939d 100644 --- a/zh-CN/src/basic-types/statements-expressions.md +++ b/zh-CN/src/basic-types/statements-expressions.md @@ -25,7 +25,7 @@ fn main() { ``` ### 练习 -🌟🌟 +1. 🌟🌟 ```rust,editable // 使用两种方法让代码工作起来 fn main() { @@ -38,7 +38,7 @@ fn main() { } ``` -🌟 +2. 🌟 ```rust,editable fn main() { @@ -48,7 +48,7 @@ fn main() { } ``` -🌟 +3. 🌟 ```rust,editable fn main() { diff --git a/zh-CN/src/compound-types/array.md b/zh-CN/src/compound-types/array.md index 5e9e710..afd9413 100644 --- a/zh-CN/src/compound-types/array.md +++ b/zh-CN/src/compound-types/array.md @@ -8,7 +8,7 @@ fn create_arr(n: i32) { 以上函数将报错,因为编译器无法在编译期知道 `n` 的具体大小。 -🌟 +1. 🌟 ```rust,editable fn main() { @@ -20,7 +20,7 @@ fn main() { } ``` -🌟🌟 +2. 🌟🌟 ```rust,editable fn main() { @@ -35,7 +35,7 @@ fn main() { } ``` -🌟 数组中的所有元素可以一起初始化为同一个值 +3. 🌟 数组中的所有元素可以一起初始化为同一个值 ```rust,editable fn main() { @@ -47,7 +47,7 @@ fn main() { } ``` -🌟 数组中的所有元素必须是同一类型 +4. 🌟 数组中的所有元素必须是同一类型 ```rust,editable fn main() { @@ -56,7 +56,7 @@ fn main() { } ``` -🌟 数组的下标索引从 0 开始. +5. 🌟 数组的下标索引从 0 开始. ```rust,editable fn main() { @@ -68,7 +68,7 @@ fn main() { } ``` -🌟 越界索引会导致代码的 `panic`. +6. 🌟 越界索引会导致代码的 `panic`. ```rust,editable // 修复代码中的错误 diff --git a/zh-CN/src/compound-types/enum.md b/zh-CN/src/compound-types/enum.md index 7733b3b..3463c19 100644 --- a/zh-CN/src/compound-types/enum.md +++ b/zh-CN/src/compound-types/enum.md @@ -1,5 +1,5 @@ # 枚举 Enum -🌟🌟 在创建枚举时,你可以使用显式的整数设定枚举成员的值。 +1. 🌟🌟 在创建枚举时,你可以使用显式的整数设定枚举成员的值。 ```rust,editable @@ -31,7 +31,7 @@ fn main() { } ``` -🌟 枚举成员可以持有各种类型的值 +2. 🌟 枚举成员可以持有各种类型的值 ```rust,editable // 填空 @@ -48,7 +48,7 @@ fn main() { } ``` -🌟🌟 枚举成员中的值可以使用模式匹配来获取 +3. 🌟🌟 枚举成员中的值可以使用模式匹配来获取 ```rust,editable // 仅填空并修复错误 @@ -70,7 +70,7 @@ fn main() { } ``` -🌟🌟 使用枚举对类型进行同一化 +4. 🌟🌟 使用枚举对类型进行同一化 ```rust,editable @@ -99,7 +99,7 @@ fn show_message(msg: Message) { } ``` -🌟🌟 Rust 中没有 `null`,我们通过 `Option` 枚举来处理值为空的情况 +5. 🌟🌟 Rust 中没有 `null`,我们通过 `Option` 枚举来处理值为空的情况 ```rust,editable // 填空让 `println` 输出,同时添加一些代码不要让最后一行的 `panic` 执行到 @@ -124,7 +124,7 @@ fn plus_one(x: Option) -> Option { ``` -🌟🌟🌟🌟 使用枚举来实现链表. +6. 🌟🌟🌟🌟 使用枚举来实现链表. ```rust,editable diff --git a/zh-CN/src/compound-types/slice.md b/zh-CN/src/compound-types/slice.md index eacd553..c0fdbc5 100644 --- a/zh-CN/src/compound-types/slice.md +++ b/zh-CN/src/compound-types/slice.md @@ -1,7 +1,7 @@ # 切片( Slice ) 切片跟数组相似,但是切片的长度无法在编译期得知,因此你无法直接使用切片类型。 -🌟🌟 这里, `[i32]` 和 `str` 都是切片类型,但是直接使用它们会造成编译错误,如下代码所示。为了解决,你需要使用切片的引用: `&[i32]`, `&str`. +1. 🌟🌟 这里, `[i32]` 和 `str` 都是切片类型,但是直接使用它们会造成编译错误,如下代码所示。为了解决,你需要使用切片的引用: `&[i32]`, `&str`. ```rust,editable // 修复代码中的错误,不要新增代码行! @@ -17,7 +17,7 @@ fn main() { 切片( 引用 )可以用来借用数组的某个连续的部分,对应的签名是 `&[T]`,大家可以与数组的签名对比下 `[T; Length]`。 -🌟🌟🌟 +2. 🌟🌟🌟 ```rust,editable fn main() { @@ -31,7 +31,7 @@ fn main() { } ``` -🌟🌟 +3. 🌟🌟 ```rust,editable fn main() { @@ -43,7 +43,7 @@ fn main() { ``` ### 字符串切片 -🌟 +4. 🌟 ```rust,editable fn main() { @@ -57,7 +57,7 @@ fn main() { } ``` -🌟 +5. 🌟 ```rust,editable fn main() { @@ -69,7 +69,7 @@ fn main() { } ``` -🌟🌟 `&String` 可以被隐式地转换成 `&str` 类型. +6. 🌟🌟 `&String` 可以被隐式地转换成 `&str` 类型. ```rust,editable // 修复所有错误 diff --git a/zh-CN/src/compound-types/string.md b/zh-CN/src/compound-types/string.md index 9766653..abe267d 100644 --- a/zh-CN/src/compound-types/string.md +++ b/zh-CN/src/compound-types/string.md @@ -3,7 +3,7 @@ ### `str` 和 `&str` -🌟 正常情况下我们无法使用 `str` 类型,但是可以使用 `&str` 来替代 +1. 🌟 正常情况下我们无法使用 `str` 类型,但是可以使用 `&str` 来替代 ```rust,editable @@ -14,7 +14,7 @@ fn main() { ``` -🌟🌟 如果要使用 `str` 类型,只能配合 `Box`。 `&` 可以用来将 `Box` 转换为 `&str` 类型 +2. 🌟🌟 如果要使用 `str` 类型,只能配合 `Box`。 `&` 可以用来将 `Box` 转换为 `&str` 类型 ```rust,editable @@ -32,7 +32,7 @@ fn greetings(s: &str) { ### String `String` 是定义在标准库中的类型,分配在堆上,可以动态的增长。它的底层存储是动态字节数组的方式( `Vec` ),但是与字节数组不同,`String` 是 `UTF-8` 编码。 -🌟 +3. 🌟 ```rust,editable // 填空 @@ -45,7 +45,7 @@ fn main() { } ``` -🌟🌟🌟 +4. 🌟🌟🌟 ```rust,editable // 修复所有错误,并且不要新增代码行 @@ -59,7 +59,7 @@ fn main() { } ``` -🌟🌟 我们可以用 `replace` 方法来替换指定的子字符串 +5. 🌟🌟 我们可以用 `replace` 方法来替换指定的子字符串 ```rust,editable // 填空 @@ -75,7 +75,7 @@ fn main() { 在标准库的 [String](https://doc.rust-lang.org/std/string/struct.String.html) 模块中,有更多的实用方法,感兴趣的同学可以看看。 -🌟🌟 你只能将 `String` 跟 `&str` 类型进行拼接,并且 `String` 的所有权在此过程中会被 move +6. 🌟🌟 你只能将 `String` 跟 `&str` 类型进行拼接,并且 `String` 的所有权在此过程中会被 move ```rust,editable @@ -93,7 +93,7 @@ fn main() { 与 `str` 的很少使用相比,`&str` 和 `String` 类型却非常常用,因此也非常重要。 -🌟🌟 我们可以使用两种方法将 `&str` 转换成 `String` 类型 +7. 🌟🌟 我们可以使用两种方法将 `&str` 转换成 `String` 类型 ```rust,editable // 使用至少两种方法来修复错误 @@ -107,7 +107,7 @@ fn greetings(s: String) { } ``` -🌟🌟 我们可以使用 `String::from` 或 `to_string` 将 `&str` 转换成 `String` 类型 +8. 🌟🌟 我们可以使用 `String::from` 或 `to_string` 将 `&str` 转换成 `String` 类型 ```rust,editable @@ -119,7 +119,7 @@ fn main() { ``` ### 字符串转义 -🌟 +9. 🌟 ```rust,editable fn main() { // 你可以使用转义的方式来输出想要的字符,这里我们使用十六进制的值,例如 \x73 会被转义成小写字母 's' @@ -144,7 +144,7 @@ fn main() { ``` -🌟🌟🌟 有时候需要转义的字符很多,我们会希望使用更方便的方式来书写字符串: raw string. +10. 🌟🌟🌟 有时候需要转义的字符很多,我们会希望使用更方便的方式来书写字符串: raw string. ```rust,editable fn main() { @@ -214,7 +214,7 @@ fn main() { 如果大家想要了解更多关于字符串字面量、转义字符的话,可以看看 Rust Reference 的 ['Tokens' 章节](https://doc.rust-lang.org/reference/tokens.html). ### 字符串索引string index -🌟🌟 你无法通过索引的方式去访问字符串中的某个字符,但是可以使用切片的方式 `&s1[start..end]` ,但是`start` 和 `end` 必须准确落在字符的边界处. +11. 🌟🌟 你无法通过索引的方式去访问字符串中的某个字符,但是可以使用切片的方式 `&s1[start..end]` ,但是`start` 和 `end` 必须准确落在字符的边界处. ```rust,editable @@ -229,7 +229,7 @@ fn main() { ``` ### 操作 UTF-8 字符串 -🌟 +12. 🌟 ```rust,editable fn main() { diff --git a/zh-CN/src/compound-types/struct.md b/zh-CN/src/compound-types/struct.md index e0fe9f5..51458b1 100644 --- a/zh-CN/src/compound-types/struct.md +++ b/zh-CN/src/compound-types/struct.md @@ -1,7 +1,7 @@ # 结构体 ### 三种类型的结构体 -🌟 对于结构体,我们必须为其中的每一个字段都指定具体的值 +1. 🌟 对于结构体,我们必须为其中的每一个字段都指定具体的值 ```rust,editable // fix the error @@ -20,7 +20,7 @@ fn main() { ``` -🌟 单元结构体没有任何字段。 +2. 🌟 单元结构体没有任何字段。 ```rust,editable struct Unit; @@ -40,7 +40,7 @@ fn main() { fn do_something_with_unit(u: __) { } ``` -🌟🌟🌟 元组结构体看起来跟元组很像,但是它拥有一个结构体的名称,该名称可以赋予它一定的意义。由于它并不关心内部数据到底是什么名称,因此此时元组结构体就非常适合。 +3. 🌟🌟🌟 元组结构体看起来跟元组很像,但是它拥有一个结构体的名称,该名称可以赋予它一定的意义。由于它并不关心内部数据到底是什么名称,因此此时元组结构体就非常适合。 ```rust,editable @@ -61,7 +61,7 @@ fn check_color(p: Color) { ``` ### 结构体上的一些操作 -🌟 你可以在实例化一个结构体时将它整体标记为可变的,但是 Rust 不允许我们将结构体的某个字段专门指定为可变的. +4. 🌟 你可以在实例化一个结构体时将它整体标记为可变的,但是 Rust 不允许我们将结构体的某个字段专门指定为可变的. ```rust,editable @@ -85,7 +85,7 @@ fn main() { } ``` -🌟 使用结构体字段初始化缩略语法可以减少一些重复代码 +5. 🌟 使用结构体字段初始化缩略语法可以减少一些重复代码 ```rust,editable // 填空 @@ -103,7 +103,7 @@ fn build_person(name: String, age: u8) -> Person { } ``` -🌟 你可以使用结构体更新语法基于一个结构体实例来构造另一个 +6. 🌟 你可以使用结构体更新语法基于一个结构体实例来构造另一个 ```rust,editable // 填空,让代码工作 @@ -133,7 +133,7 @@ fn set_email(u: User) -> User { ``` ### 打印结构体 -🌟🌟 我们可以使用 `#[derive(Debug)]` 让[结构体变成可打印的](https://course.rs/basic/compound-type/struct.html#使用-derivedebug-来打印结构体的信息). +7. 🌟🌟 我们可以使用 `#[derive(Debug)]` 让[结构体变成可打印的](https://course.rs/basic/compound-type/struct.html#使用-derivedebug-来打印结构体的信息). ```rust,editable @@ -195,7 +195,7 @@ fn main() { #### 练习 -🌟🌟 +8. 🌟🌟 ```rust,editable // 修复错误 diff --git a/zh-CN/src/compound-types/tuple.md b/zh-CN/src/compound-types/tuple.md index 60597bb..840f637 100644 --- a/zh-CN/src/compound-types/tuple.md +++ b/zh-CN/src/compound-types/tuple.md @@ -1,5 +1,5 @@ # 元组( Tuple ) -🌟 元组中的元素可以是不同的类型。元组的类型签名是 `(T1, T2, ...)`, 这里 `T1`, `T2` 是相对应的元组成员的类型. +1. 🌟 元组中的元素可以是不同的类型。元组的类型签名是 `(T1, T2, ...)`, 这里 `T1`, `T2` 是相对应的元组成员的类型. ```rust,editable fn main() { @@ -11,7 +11,7 @@ fn main() { } ``` -🌟 可以使用索引来获取元组的成员 +2. 🌟 可以使用索引来获取元组的成员 ```rust,editable // 修改合适的地方,让代码工作 @@ -21,7 +21,7 @@ fn main() { } ``` -🌟 过长的元组无法被打印输出 +3. 🌟 过长的元组无法被打印输出 ```rust,editable // 修复代码错误 @@ -31,7 +31,7 @@ fn main() { } ``` -🌟 使用模式匹配来解构元组 +4. 🌟 使用模式匹配来解构元组 ```rust,editable fn main() { @@ -46,7 +46,7 @@ fn main() { } ``` -🌟🌟 解构式赋值 +5. 🌟🌟 解构式赋值 ```rust,editable fn main() { let (x, y, z); @@ -60,7 +60,7 @@ fn main() { } ``` -🌟🌟 元组可以用于函数的参数和返回值 +6. 🌟🌟 元组可以用于函数的参数和返回值 ```rust,editable fn main() { diff --git a/zh-CN/src/flow-control.md b/zh-CN/src/flow-control.md index b986b87..555fcd8 100644 --- a/zh-CN/src/flow-control.md +++ b/zh-CN/src/flow-control.md @@ -1,7 +1,7 @@ # 流程控制 ### if/else -🌟 +1. 🌟 ```rust,editable // 填空 @@ -18,7 +18,7 @@ fn main() { } ``` -🌟🌟 if/else 可以用作表达式来进行赋值 +2. 🌟🌟 if/else 可以用作表达式来进行赋值 ```rust,editable // 修复错误 @@ -41,7 +41,7 @@ fn main() { ``` ### for -🌟 `for in` 可以用于迭代一个迭代器,例如序列 `a..b`. +3. 🌟 `for in` 可以用于迭代一个迭代器,例如序列 `a..b`. ```rust,editable @@ -55,7 +55,7 @@ fn main() { ``` -🌟🌟 +4. 🌟🌟 ```rust,editable // 修复错误,不要新增或删除代码行 @@ -77,7 +77,7 @@ fn main() { } ``` -🌟 +5. 🌟 ```rust,editable fn main() { let a = [4,3,2,1]; @@ -90,7 +90,7 @@ fn main() { ``` ### while -🌟🌟 当条件为 true 时,`while` 将一直循环 +6. 🌟🌟 当条件为 true 时,`while` 将一直循环 ```rust,editable @@ -120,7 +120,7 @@ fn main() { ``` ### continue and break -🌟 使用 `break` 可以跳出循环 +7. 🌟 使用 `break` 可以跳出循环 ```rust,editable // 填空,不要修改其它代码 @@ -137,7 +137,7 @@ fn main() { } ``` -🌟🌟 `continue` 会结束当次循环并立即开始下一次循环 +8. 🌟🌟 `continue` 会结束当次循环并立即开始下一次循环 ```rust,editable // 填空,不要修改其它代码 @@ -158,7 +158,7 @@ fn main() { ### loop -🌟🌟 loop 一般都需要配合 `break` 或 `continue` 一起使用。 +9. 🌟🌟 loop 一般都需要配合 `break` 或 `continue` 一起使用。 ```rust,editable @@ -192,7 +192,7 @@ fn main() { } ``` -🌟🌟 loop 是一个表达式,因此我们可以配合 `break` 来返回一个值 +10. 🌟🌟 loop 是一个表达式,因此我们可以配合 `break` 来返回一个值 ```rust,editable // 填空 @@ -211,7 +211,7 @@ fn main() { } ``` -🌟🌟🌟 当有多层循环时,你可以使用 `continue` 或 `break` 来控制外层的循环。要实现这一点,外部的循环必须拥有一个标签 `'label`, 然后在 `break` 或 `continue` 时指定该标签 +11. 🌟🌟🌟 当有多层循环时,你可以使用 `continue` 或 `break` 来控制外层的循环。要实现这一点,外部的循环必须拥有一个标签 `'label`, 然后在 `break` 或 `continue` 时指定该标签 ```rust,editable diff --git a/zh-CN/src/ownership/borrowing.md b/zh-CN/src/ownership/borrowing.md index d1caac2..2af1dd9 100644 --- a/zh-CN/src/ownership/borrowing.md +++ b/zh-CN/src/ownership/borrowing.md @@ -1,7 +1,7 @@ # 引用和借用 ### 引用 -🌟 +1. 🌟 ```rust,editable fn main() { @@ -13,7 +13,7 @@ fn main() { } ``` -🌟 +2. 🌟 ```rust,editable fn main() { @@ -25,7 +25,7 @@ fn main() { } ``` -🌟 +3. 🌟 ```rust,editable // 修复错误 @@ -38,7 +38,7 @@ fn main() { fn borrow_object(s: &String) {} ``` -🌟 +4. 🌟 ```rust,editable // 修复错误 @@ -53,7 +53,7 @@ fn push_str(s: &mut String) { } ``` -🌟🌟 +5. 🌟🌟 ```rust,editable fn main() { @@ -69,7 +69,7 @@ fn main() { #### ref `ref` 与 `&` 类似,可以用来获取一个值的引用,但是它们的用法有所不同。 -🌟🌟🌟 +6. 🌟🌟🌟 ```rust,editable fn main() { @@ -92,7 +92,7 @@ fn get_addr(r: &char) -> String { ``` ### 借用规则 -🌟 +7. 🌟 ```rust,editable // 移除代码某个部分,让它工作 @@ -108,7 +108,7 @@ fn main() { ``` #### 可变性 -🌟 错误: 从可不用对象借用可用 +8. 🌟 错误: 从可不用对象借用可用 ```rust,editable fn main() { @@ -121,7 +121,7 @@ fn main() { fn borrow_object(s: &mut String) {} ``` -🌟🌟 Ok: 从可变对象借用不可变 +9. 🌟🌟 Ok: 从可变对象借用不可变 ```rust,editable // 下面的代码没有任何错误 @@ -137,7 +137,7 @@ fn borrow_object(s: &String) {} ``` ### NLL -🌟🌟 +10. 🌟🌟 ```rust,editable // 注释掉一行代码让它工作 @@ -153,7 +153,7 @@ fn main() { } ``` -🌟🌟 +11. 🌟🌟 ```rust,editable fn main() { diff --git a/zh-CN/src/ownership/ownership.md b/zh-CN/src/ownership/ownership.md index df71e21..fc8ad3d 100644 --- a/zh-CN/src/ownership/ownership.md +++ b/zh-CN/src/ownership/ownership.md @@ -1,6 +1,6 @@ # 所有权 -🌟🌟 +1. 🌟🌟 ```rust,editable fn main() { @@ -11,7 +11,7 @@ fn main() { } ``` -🌟🌟 +2. 🌟🌟 ```rust,editable // 不要修改 main 中的代码 fn main() { @@ -28,7 +28,7 @@ fn take_ownership(s: String) { ``` -🌟🌟 +3. 🌟🌟 ```rust,editable fn main() { @@ -46,7 +46,7 @@ fn give_ownership() -> String { } ``` -🌟🌟 +4. 🌟🌟 ```rust,editable // 修复错误,不要删除任何代码行 fn main() { @@ -62,7 +62,7 @@ fn print_str(s: String) { } ``` -🌟🌟 +5. 🌟🌟 ```rust, editable // 不要使用 clone,使用 copy 的方式替代 fn main() { @@ -75,7 +75,7 @@ fn main() { #### 可变性 当所有权转移时,可变性也可以随之改变。 -🌟 +6. 🌟 ```rust,editable fn main() { @@ -88,7 +88,7 @@ fn main() { } ``` -🌟🌟🌟 +7. 🌟🌟🌟 ```rust,editable fn main() { @@ -140,7 +140,7 @@ fn main() { #### 练习 -🌟 +8. 🌟 ```rust,editable fn main() { @@ -153,7 +153,7 @@ fn main() { } ``` -🌟🌟 +9. 🌟🌟 ```rust,editable fn main() { diff --git a/zh-CN/src/pattern-match/match-iflet.md b/zh-CN/src/pattern-match/match-iflet.md index 9f58a52..922e4b3 100644 --- a/zh-CN/src/pattern-match/match-iflet.md +++ b/zh-CN/src/pattern-match/match-iflet.md @@ -1,7 +1,7 @@ # match, matches! 和 if let ### match -🌟🌟 +1. 🌟🌟 ```rust,editable // 填空 @@ -24,7 +24,7 @@ fn main() { } ``` -🌟🌟 `match` 是一个表达式,因此可以用在赋值语句中 +2. 🌟🌟 `match` 是一个表达式,因此可以用在赋值语句中 ```rust,editable fn main() { @@ -40,7 +40,7 @@ fn main() { } ``` -🌟🌟 使用 match 匹配出枚举成员持有的值 +3. 🌟🌟 使用 match 匹配出枚举成员持有的值 ```rust,editable // 填空 @@ -81,7 +81,7 @@ fn show_message(msg: Message) { ### matches! [`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) 看起来像 `match`, 但是它可以做一些特别的事情 -🌟🌟 +4. 🌟🌟 ```rust,editable fn main() { @@ -94,7 +94,7 @@ fn main() { } ``` -🌟🌟 +5. 🌟🌟 ```rust,editable enum MyEnum { @@ -119,7 +119,7 @@ fn main() { ### if let 在有些时候, 使用 `match` 匹配枚举有些太重了,此时 `if let` 就非常适合. -🌟 +6. 🌟 ```rust,editable fn main() { @@ -135,7 +135,7 @@ fn main() { } ``` -🌟🌟 +7. 🌟🌟 ```rust,editable // 填空 @@ -152,7 +152,7 @@ fn main() { } ``` -🌟🌟 +8. 🌟🌟 ```rust,editable enum Foo { @@ -176,7 +176,7 @@ fn main() { ``` ### 变量遮蔽( Shadowing ) -🌟🌟 +9. 🌟🌟 ```rust,editable // 就地修复错误 diff --git a/zh-CN/src/pattern-match/patterns.md b/zh-CN/src/pattern-match/patterns.md index dd31d91..d45c688 100644 --- a/zh-CN/src/pattern-match/patterns.md +++ b/zh-CN/src/pattern-match/patterns.md @@ -1,6 +1,6 @@ # 模式 -🌟🌟 使用 `|` 可以匹配多个值, 而使用 `..=` 可以匹配一个闭区间的数值序列 +1. 🌟🌟 使用 `|` 可以匹配多个值, 而使用 `..=` 可以匹配一个闭区间的数值序列 ```rust,editable fn main() {} @@ -21,7 +21,7 @@ fn match_number(n: i32) { } ``` -🌟🌟🌟 `@` 操作符可以让我们将一个与模式相匹配的值绑定到新的变量上 +2. 🌟🌟🌟 `@` 操作符可以让我们将一个与模式相匹配的值绑定到新的变量上 ```rust,editable struct Point { @@ -42,7 +42,7 @@ fn main() { } ``` -🌟🌟🌟 +3. 🌟🌟🌟 ```rust,editable @@ -66,7 +66,7 @@ fn main() { } ``` -🌟🌟 匹配守卫(match guard)是一个位于 match 分支模式之后的额外 if 条件,它能为分支模式提供更进一步的匹配条件。 +4. 🌟🌟 匹配守卫(match guard)是一个位于 match 分支模式之后的额外 if 条件,它能为分支模式提供更进一步的匹配条件。 ```rust,editable // 填空让代码工作,必须使用 `split` @@ -81,7 +81,7 @@ fn main() { } ``` -🌟🌟🌟 使用 `..` 忽略一部分值 +5. 🌟🌟🌟 使用 `..` 忽略一部分值 ```rust,editable // 填空,让代码工作 diff --git a/zh-CN/src/variables.md b/zh-CN/src/variables.md index b2193e0..0b6b445 100644 --- a/zh-CN/src/variables.md +++ b/zh-CN/src/variables.md @@ -1,7 +1,7 @@ # 变量绑定与解构 ### 绑定和可变性 -🌟 变量只有在初始化后才能被使用 +1. 🌟 变量只有在初始化后才能被使用 ```rust,editable @@ -13,7 +13,7 @@ fn main() { } ``` -🌟🌟 可以使用 `mut` 将变量标记为可变 +2. 🌟🌟 可以使用 `mut` 将变量标记为可变 ```rust,editable // 完形填空,让代码编译 @@ -26,7 +26,7 @@ fn main() { ``` ### 变量作用域 -🌟 作用域是一个变量在程序中能够保持合法的范围 +3. 🌟 作用域是一个变量在程序中能够保持合法的范围 ```rust,editable @@ -41,7 +41,7 @@ fn main() { } ``` -🌟🌟 +4. 🌟🌟 ```rust,editable // 修复错误 @@ -55,7 +55,7 @@ fn define_x() { ``` ### 变量遮蔽( Shadowing ) -🌟🌟 若后面的变量声明的名称和之前的变量相同,则我们说:第一个变量被第二个同名变量遮蔽了( shadowing ) +5. 🌟🌟 若后面的变量声明的名称和之前的变量相同,则我们说:第一个变量被第二个同名变量遮蔽了( shadowing ) ```rust,editable @@ -74,7 +74,7 @@ fn main() { } ``` -🌟🌟 删除一行代码以通过编译 +6. 🌟🌟 删除一行代码以通过编译 ```rust,editable fn main() { @@ -92,7 +92,7 @@ fn main() { ``` ### 未使用的变量 -使用以下方法来修复编译器输出的 warning : +7. 使用以下方法来修复编译器输出的 warning : - 🌟 一种方法 - 🌟🌟 两种方法 @@ -109,7 +109,7 @@ fn main() { ``` ### 变量解构 -🌟🌟 我们可以将 `let` 跟一个模式一起使用来解构一个元组,最终将它解构为多个独立的变量 +8. 🌟🌟 我们可以将 `let` 跟一个模式一起使用来解构一个元组,最终将它解构为多个独立的变量 > 提示: 可以使用变量遮蔽或可变性 @@ -128,7 +128,7 @@ fn main() { ### 解构式赋值 该功能于 Rust 1.59 版本引入:你可以在赋值语句的左式中使用元组、切片或结构体进行匹配赋值。 -🌟🌟 +9. 🌟🌟 > Note: 解构式赋值只能在 Rust 1.59 或者更高版本中使用