diff --git a/06-flow/06-02-condition.md b/06-flow/06-02-condition.md index f8d6e14..29374c6 100644 --- a/06-flow/06-02-condition.md +++ b/06-flow/06-02-condition.md @@ -76,7 +76,7 @@ let z = if let Some(y) = x { } else { 0 -} +}; // z 值为 5 ``` @@ -93,7 +93,7 @@ match x { let z = match x { Some(y) => y, None => 0 -} +}; ``` 设计这个语法的目的是,在条件判断的时候,直接做一次模式匹配,方便代码书写,使代码更紧凑。 diff --git a/07-type/07-02-compound-types.md b/07-type/07-02-compound-types.md index 6328ebd..ee23a56 100644 --- a/07-type/07-02-compound-types.md +++ b/07-type/07-02-compound-types.md @@ -6,7 +6,7 @@ ```rust let y = (2, "hello world"); -let x: (3, &str) = (3, "world hello"); +let x: (i32, &str) = (3, "world hello"); // 然后呢,你能用很简单的方式去访问他们: diff --git a/08-function/08-02-return_value.md b/08-function/08-02-return_value.md index 967f742..ba08644 100644 --- a/08-function/08-02-return_value.md +++ b/08-function/08-02-return_value.md @@ -20,10 +20,10 @@ ```rust fn main() { let a = 3; - println!("{}", inc(3)); + println!("{}", inc(a)); } - fn inc(n: i32) -> i32 { + fn inc(n: i32) -> i32 { n + 1 } ``` diff --git a/09-match/09-02-pattern.md b/09-match/09-02-pattern.md index 60cbd9d..68e679c 100644 --- a/09-match/09-02-pattern.md +++ b/09-match/09-02-pattern.md @@ -1,7 +1,7 @@ # 模式 模式,是Rust另一个强大的语法。它可以被用在`let`和`match`表达式里面。相信大家应该还记得我们在[基础类型](../07-type/07-01-types.md)中提到的关于在let表达式中解构元组的例子,实际上这就是一个模式。 ```rust -let tup = (0u9,1u8); +let tup = (0u8,1u8); let (x, y) = tup; ``` 而且我们需要知道的是,一个模式中如果出现了和前面的同名变量,则这个变量会在当前作用域里被模式中的值覆盖掉。比如: @@ -73,7 +73,7 @@ match point { ## 忽略和内存管理 -总结以下,我们遇到了两种不同的模式忽略的情况——`_`和`..`。这里要注意,模式匹配中被忽略的字段是不会被`move`的,而且实现`Copy`的也会优先被Copy而不是被`move`。 +总结一下,我们遇到了两种不同的模式忽略的情况——`_`和`..`。这里要注意,模式匹配中被忽略的字段是不会被`move`的,而且实现`Copy`的也会优先被Copy而不是被`move`。 说的有点拗口,上代码: @@ -87,7 +87,7 @@ let (x, s) = tuple; let tuple = (5, String::from("five")); -/// 忽略String类型,而u32实现了Copy,则tuple不会被move +// 忽略String类型,而u32实现了Copy,则tuple不会被move let (x, _) = tuple; println!("Tuple is: {:?}", tuple); @@ -113,8 +113,8 @@ let c = 'w'; match c { 'a' ... 'z' => println!("小写字母"), - 'A' ... 'Z' => pringln!("大写字母"), - _ => pringln!("其他字符"), + 'A' ... 'Z' => println!("大写字母"), + _ => println!("其他字符"), } ``` diff --git a/12-ownership-system/12-01-ownership.md b/12-ownership-system/12-01-ownership.md index 555f53b..4c93f49 100644 --- a/12-ownership-system/12-01-ownership.md +++ b/12-ownership-system/12-01-ownership.md @@ -201,7 +201,7 @@ struct Bar { //不可实现Copy特性 1. **通过derive让Rust编译器自动实现** ```rust -#derive(Copy, Clone)] +#[derive(Copy, Clone)] struct Foo { a: i32, b: bool,