Merge pull request #324 from katopz/master

fix: change 、to .
This commit is contained in:
Sunface 2022-12-07 08:49:37 +08:00 committed by GitHub
commit 2c52ef2e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 159 additions and 116 deletions

View File

@ -41,7 +41,9 @@ Closures can capture variables by borrowing or moving. But they prefer to captur
- By mutable reference: `&mut T`
- By value: `T`
1.🌟
1. 🌟
```rust,editable
/* Make it work with least amount of changes*/
fn main() {
@ -60,7 +62,9 @@ fn main() {
}
```
2.🌟🌟
2. 🌟🌟
```rust,editable
/* Make it work
- Dont use `_reborrow` and `_count_reborrowed`
@ -89,7 +93,9 @@ fn main() {
}
```
3.🌟🌟
3. 🌟🌟
```rust,editable
/* Make it work in two ways, none of them is to remove `take(movable)` away from the code
*/
@ -132,7 +138,9 @@ let add_one_v3 = |x| { x + 1 };
let add_one_v4 = |x| x + 1 ;
```
4.🌟
4. 🌟
```rust,editable
fn main() {
let example_closure = |x| x;
@ -151,7 +159,9 @@ When taking a closure as an input parameter, the closure's complete type must be
- FnMut: the closure uses the captured value by mutable reference (&mut T)
- FnOnce: the closure uses the captured value by value (T)
5.🌟🌟
5. 🌟🌟
```rust,editable
/* Make it work by changing the trait bound, in two ways*/
fn fn_once<F>(func: F)
@ -198,7 +208,9 @@ Which trait to use is determined by what the closure does with captured value.
This is because if a move is possible, then any type of borrow should also be possible. Note that the reverse is not true. If the parameter is annotated as `Fn`, then capturing variables by `&mut T` or `T` are not allowed.
7.🌟🌟
7. 🌟🌟
```rust,editable
/* Fill in the blank */
@ -285,7 +297,9 @@ fn exec<F: Fn()>(f: F) {
}
```
8.🌟🌟
8. 🌟🌟
```rust,editable
/* Fill in the blank */
fn main() {
@ -305,7 +319,9 @@ fn exec<'a, F: __>(mut f: F) {
## Input functions
Since closure can be used as arguments, you might wonder can we use functions as arguments too? And indeed we can.
9.🌟🌟
9. 🌟🌟
```rust,editable
/* Implement `call_me` to make it work */
@ -328,7 +344,9 @@ fn main() {
## Closure as return types
Returning a closure is much harder than you may have thought of.
10.🌟🌟
10. 🌟🌟
```rust,editable
/* Fill in the blank using two aproaches,
and fix the errror */
@ -347,7 +365,9 @@ fn main() {
}
```
11.🌟🌟
11. 🌟🌟
```rust,editable
/* Fill in the blank and fix the error*/
fn factory(x:i32) -> __ {

View File

@ -23,7 +23,9 @@ fn main() {
}
```
1.🌟
1. 🌟
```rust,editable
/* Refactoring the following code using iterators */
fn main() {
@ -61,7 +63,9 @@ pub trait Iterator {
And we can call the `next` method on iterators directly.
3.🌟🌟
3. 🌟🌟
```rust,editable
/* Fill the blanks and fix the errors.
Using two ways if possible */
@ -83,7 +87,9 @@ In the previous section, we have mentioned that `for` will apply the `into_iter`
- `iter`, this borrows each element of the collection through each iteration, thus leaving the collection untouched and available for reuse after the loop
- `iter_mut`, this mutably borrows each element of the collection, allowing for the collection to be modified in place.
4.🌟
4. 🌟
```rust,editable
/* Make it work */
fn main() {
@ -96,7 +102,9 @@ fn main() {
}
```
5.🌟
5. 🌟
```rust,editable
/* Fill in the blank */
fn main() {
@ -113,7 +121,9 @@ fn main() {
}
```
6.🌟🌟
6. 🌟🌟
```rust,editable
/* Fill in the blank */
fn main() {
@ -169,7 +179,9 @@ fn main() {
}
```
7.🌟🌟🌟
7. 🌟🌟🌟
```rust,editable
struct Fibonacci {
curr: u32,
@ -208,8 +220,11 @@ The `Iterator` trait has a number of methods with default implementations provid
### Consuming adaptors
Some of these methods call the method `next`to use up the iterator, so they are called *consuming adaptors*.
8.🌟🌟
```rust,editable
8. 🌟🌟
```rust,edtiable
/* Fill in the blank and fix the errors */
fn main() {
let v1 = vec![1, 2, 3];
@ -229,7 +244,9 @@ fn main() {
#### Collect
Other than converting a collection into an iterator, we can also `collect` the result values into a collection, `collect` will consume the iterator.
9.🌟🌟
9. 🌟🌟
```rust,editable
/* Make it work */
use std::collections::HashMap;
@ -253,7 +270,9 @@ Methods allowing you to change one iterator into another iterator are known as *
But because **all iterators are lazy**, you have to call one of the consuming adapters to get results from calls to iterator adapters.
10.🌟🌟
10. 🌟🌟
```rust,editable
/* Fill in the blanks */
fn main() {
@ -265,7 +284,9 @@ fn main() {
}
```
11.🌟🌟
11. 🌟🌟
```rust
/* Fill in the blanks */
use std::collections::HashMap;
@ -281,7 +302,9 @@ fn main() {
#### Using closures in iterator adaptors
12.🌟🌟
12. 🌟🌟
```rust
/* Fill in the blanks */
#[derive(PartialEq, Debug)]

View File

@ -39,7 +39,7 @@ fn main() {
}
```
1,🌟
1. 🌟
```rust,editable
/* Annotate struct with lifetime:
1. `r` and `s` must have different lifetimes
@ -55,7 +55,7 @@ fn main() {
```
2,🌟🌟
2. 🌟🌟
```rust,editable
/* Adding trait bounds to make it work */
struct ImportantExcerpt<'a> {
@ -74,7 +74,7 @@ fn main() {
}
```
3,🌟🌟
3. 🌟🌟
```rust,editable
/* Adding trait bounds to make it work */
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) {
@ -100,7 +100,7 @@ and could then be used to compare a `&'a T` with any lifetime to an `i32`.
Only a higher-ranked bound can be used here, because the lifetime of the reference is shorter than any possible lifetime parameter on the function.
4,🌟🌟🌟
4. 🌟🌟🌟
```rust,editable
/* Adding HRTB to make it work!*/
fn call_on_ref_zero<'a, F>(f: F) where F: Fn(&'a i32) {
@ -187,7 +187,7 @@ fn main() {
```
5,🌟🌟
5. 🌟🌟
```rust,editable
/* Make it work by reordering some code */
fn main() {
@ -235,7 +235,7 @@ struct Ref<'a, T> {
## A difficult exercise
6,🌟🌟🌟🌟
6. 🌟🌟🌟🌟
```rust,editable
/* Make it work */
struct Interface<'a> {

View File

@ -2,7 +2,7 @@
The compiler uses lifetime to ensure all borrows are valid. Typically, a variable's lifetime begins when it is created and ends when it is destroyed.
## The scope of lifetime
1 🌟
1. 🌟
```rust,editable
/* Annotate the lifetime of `i` and `borrow2` */
@ -109,7 +109,7 @@ fn main() {
}
```
3 🌟
3. 🌟
```rust,editable
/* Make it work by adding proper lifetime annotation */
fn longest(x: &str, y: &str) -> &str {
@ -122,7 +122,7 @@ fn longest(x: &str, y: &str) -> &str {
fn main() {}
```
4🌟🌟🌟
4. 🌟🌟🌟
```rust,editable
// `'a` must live longer than the function.
// Here, `&String::from("foo")` would create a `String`, followed by a
@ -138,7 +138,7 @@ fn main() {
}
```
5🌟🌟
5. 🌟🌟
```rust,editable
// `print_refs` takes two references to `i32` which have different
// lifetimes `'a` and `'b`. These two lifetimes must both be at
@ -176,7 +176,7 @@ fn main() {
```
#### Structs
6 🌟
6. 🌟
```rust,editable
/* Make it work by adding proper lifetime annotation */
@ -216,7 +216,7 @@ fn main() {
```
7 🌟🌟
7. 🌟🌟
```rust,editable
/* Make it work */
@ -248,7 +248,7 @@ fn main()
```
8 🌟🌟
8. 🌟🌟
```rust,editable
#[derive(Debug)]
@ -297,7 +297,7 @@ fn main() {
}
```
9🌟🌟
9. 🌟🌟
```rust,editable
/* Make it work by adding proper lifetime annotations */
struct ImportantExcerpt {
@ -320,7 +320,7 @@ This is known as **Elision**. Elision exist in Rust only because these patterns
For a more comprehensive understanding of elision, please see [lifetime elision](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision) in the official book.
10🌟🌟
10. 🌟🌟
```rust,editable
/* Remove all the lifetimes that can be elided */

View File

@ -15,7 +15,7 @@ As a reference lifetime, `&'static` indicates the data pointed to by the referen
1🌟🌟 There are several ways to make a variable with `'static` lifetime, two of them are stored in the read-only memory of the binary.
1. 🌟🌟 There are several ways to make a variable with `'static` lifetime, two of them are stored in the read-only memory of the binary.
```rust,editable
@ -32,7 +32,7 @@ fn need_static(r : &'static str) {
}
```
2, 🌟🌟🌟🌟 Another way to make `'static` lifetime is using `Box::leak`
2. 🌟🌟🌟🌟 Another way to make `'static` lifetime is using `Box::leak`
```rust,editable
#[derive(Debug)]
struct Config {
@ -59,7 +59,7 @@ fn main() {
}
```
3, 🌟 `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
3. 🌟 `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
```rust,editable
fn main() {
{
@ -75,7 +75,7 @@ fn main() {
}
```
4, `&'static` can be coerced to a shorter lifetime.
4. `&'static` can be coerced to a shorter lifetime.
**Example**
```rust,editable
@ -112,7 +112,7 @@ As a trait bound, it means the type does not contain any non-static references.
It's important to understand this means that any owned data always passes a `'static `lifetime bound, but a reference to that owned data generally does not.
5,🌟🌟
5. 🌟🌟
```rust,editable
/* Make it work */
use std::fmt::Debug;
@ -147,7 +147,7 @@ fn main() {
```
6,🌟🌟🌟
6. 🌟🌟🌟
```rust,editable
use std::fmt::Display;

View File

@ -5,7 +5,7 @@ The orphan rule tells us that we are allowed to implement a trait on a type as l
The **newtype pattern** can help us get around this restriction, which involves creating a new type in a **tuple struct**.
1🌟
1. 🌟
```rust,editable
use std::fmt;
@ -26,7 +26,7 @@ fn main() {
}
```
2🌟 Hide the methods of the original type
2. 🌟 Hide the methods of the original type
```rust,editable
/* Make it workd */
struct Meters(u32);
@ -41,7 +41,7 @@ fn main() {
}
```
3🌟🌟 The `newtype` idiom gives compile time guarantees that the right type of value is suplied to a program.
3. 🌟🌟 The `newtype` idiom gives compile time guarantees that the right type of value is suplied to a program.
```rust,editable
/* Make it work */
struct Years(i64);
@ -74,7 +74,7 @@ fn main() {
}
```
4🌟🌟
4. 🌟🌟
```rust,editable
use std::ops::Add;
use std::fmt::{self, format};
@ -133,7 +133,7 @@ let y: Meters = 5;
println!("x + y = {}", x + y);
```
5🌟
5. 🌟
```rust,editable
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
@ -150,7 +150,7 @@ fn main() {
}
```
6🌟🌟 There are a few preserved alias in Rust, one of which can be used in `impl` blocks.
6. 🌟🌟 There are a few preserved alias in Rust, one of which can be used in `impl` blocks.
```rust,editable
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
@ -170,7 +170,7 @@ impl VeryVerboseEnumOfThingsToDoWithNumbers {
## DST and unsized type
These concepts are complicated, so we are not going to explain here, but you can find them in [The Book](https://doc.rust-lang.org/book/ch19-04-advanced-types.html?highlight=DST#dynamically-sized-types-and-the-sized-trait).
7🌟🌟🌟 Array with dynamic length is a Dynamic Sized Type ( DST ), we can't directly use it
7. 🌟🌟🌟 Array with dynamic length is a Dynamic Sized Type ( DST ), we can't directly use it
```rust,editable
/* Make it work with const generics */
fn my_function(n: usize) -> [u32; usize] {
@ -183,7 +183,7 @@ fn main() {
}
```
8🌟🌟 Slice is unsized type, but the reference of slice is not.
8. 🌟🌟 Slice is unsized type, but the reference of slice is not.
```rust,editable
/* Make it work with slice references */
fn main() {
@ -193,7 +193,7 @@ fn main() {
}
```
9🌟🌟 Trait is also a unsized type
9. 🌟🌟 Trait is also a unsized type
```rust,editable
/* Make it work in two ways */
use std::fmt::Display;

View File

@ -42,7 +42,7 @@ pub fn add_three(x: i32) -> Option<i32> {
}
mod a {
/// Add four to the given value and return a [`Option`] type
/// Add four to the given value and return a [`Option`] type
/// [`crate::MySpecialFormatter`]
pub fn add_four(x: i32) -> Option<i32> {
Some(x + 4)

View File

@ -1,4 +1,4 @@
1
1.
```rust
fn main() {
@ -13,7 +13,7 @@ fn main() {
}
```
2
2.
```rust
fn main() {
@ -39,7 +39,7 @@ fn main() {
}
```
3
3.
```rust
fn main() {
@ -85,7 +85,7 @@ fn take<T>(_v: &T) {
}
```
4
4.
```rust
fn main() {
@ -98,7 +98,7 @@ fn main() {
}
```
5
5.
```rust
fn fn_once<F>(func: F)
@ -130,7 +130,7 @@ fn main() {
}
```
6
6.
```rust
fn main() {
@ -148,7 +148,7 @@ fn exec<'a, F: FnMut(&'a str)>(mut f: F) {
}
```
7
7.
```rust
// A function which takes a closure as an argument and calls it.
@ -203,7 +203,7 @@ fn main() {
}
```
8
8.
```rust
fn main() {
@ -219,7 +219,7 @@ fn exec<'a, F: FnOnce(&'a str) -> String>(mut f: F) {
}
```
9
9.
```rust
// Define a function which takes a generic `F` argument
@ -242,7 +242,7 @@ fn main() {
}
```
10
10.
```rust
/* Fill in the blank and fix the errror */
@ -277,7 +277,7 @@ fn main() {
}
```
11
11.
```rust
// Every closure has its own type. Even if one closure has the same representation as another, their types are different.

View File

@ -1,4 +1,4 @@
1
1.
```rust
fn main() {
@ -9,7 +9,7 @@ fn main() {
}
```
2
2.
```rust
fn main() {
@ -22,7 +22,7 @@ fn main() {
}
```
3
3.
```rust
fn main() {
@ -50,7 +50,7 @@ fn main() {
}
```
4
4.
```rust
fn main() {
@ -63,7 +63,7 @@ fn main() {
}
```
5
5.
```rust
fn main() {
@ -80,7 +80,7 @@ fn main() {
}
```
6
6.
```rust
fn main() {
@ -95,7 +95,7 @@ fn main() {
}
```
7
7.
```rust
struct Fibonacci {
@ -142,7 +142,7 @@ fn main() {
}
```
8
8.
```rust
fn main() {
@ -159,7 +159,7 @@ fn main() {
}
```
9
9.
```rust
use std::collections::HashMap;
@ -177,7 +177,7 @@ fn main() {
}
```
10
10.
```rust
fn main() {
@ -189,7 +189,7 @@ fn main() {
}
```
11
11.
```rust
use std::collections::HashMap;
@ -202,7 +202,7 @@ fn main() {
}
```
12
12.
```rust
#[derive(PartialEq, Debug)]

View File

@ -1,4 +1,4 @@
1
1.
```rust
struct DoubleRef<'a,'b:'a, T> {
@ -10,7 +10,7 @@ fn main() {
}
```
2
2.
```rust
struct ImportantExcerpt<'a> {
@ -29,7 +29,7 @@ fn main() {
}
```
3
3.
```rust
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) where 'a: 'b {
@ -41,7 +41,7 @@ fn main() {
}
```
4
4.
```rust
fn call_on_ref_zero<F>(f: F) where for<'a> F: Fn(&'a i32) {
@ -65,7 +65,7 @@ fn call_on_ref_zero<F>(f: F) where F: for<'a> Fn(&'a i32) {
}
```
5
5.
```rust
fn main() {
@ -80,7 +80,7 @@ fn main() {
}
```
6
6.
```rust
struct Interface<'b, 'a: 'b> {

View File

@ -1,4 +1,4 @@
1
1.
```rust
fn main() {
@ -26,7 +26,7 @@ fn need_static(r : &'static str) {
}
```
2
2.
```rust
#[derive(Debug)]
@ -55,7 +55,7 @@ fn main() {
}
```
3
3.
```rust
fn main() {
@ -67,7 +67,7 @@ fn main() {
}
```
5
5.
```rust
use std::fmt::Debug;

View File

@ -1,4 +1,4 @@
1
1.
```rust
use std::fmt;
@ -17,7 +17,7 @@ fn main() {
}
```
2
2.
```rust
struct Meters(u32);
@ -31,7 +31,7 @@ fn main() {
}
```
3
3.
```rust
struct Years(i64);
@ -64,7 +64,7 @@ fn main() {
}
```
4Sometimes `newtype` pattern can provide extra readability.
4. Sometimes `newtype` pattern can provide extra readability.
```rust
use std::ops::Add;
@ -97,7 +97,7 @@ fn calculate_distance(d1: Meters, d2: Meters) -> Meters {
}
```
5
5.
```rust
enum VeryVerboseEnumOfThingsToDoWithNumbers {
@ -115,7 +115,7 @@ fn main() {
}
```
6
6.
```rust
enum VeryVerboseEnumOfThingsToDoWithNumbers {
@ -135,7 +135,7 @@ impl VeryVerboseEnumOfThingsToDoWithNumbers {
fn main() {}
```
7
7.
```rust
fn my_function<const N: usize>() -> [u32; N] {
@ -148,7 +148,7 @@ fn main() {
}
```
8
8.
```rust
fn main() {
@ -158,7 +158,7 @@ fn main() {
}
```
9
9.
```rust
use std::fmt::Display;

View File

@ -39,7 +39,7 @@ fn main() {
}
```
1🌟
1. 🌟
```rust,editable
/* 使用生命周期注释结构体
1. `r``s` 必须是不同生命周期
@ -55,7 +55,7 @@ fn main() {
```
2🌟🌟
2. 🌟🌟
```rust,editable
/* 添加类型约束使下面代码正常运行 */
struct ImportantExcerpt<'a> {
@ -74,7 +74,7 @@ fn main() {
}
```
3🌟🌟
3. 🌟🌟
```rust,editable
/* 添加类型约束使下面代码正常运行 */
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) {
@ -100,7 +100,7 @@ impl<'a> PartialEq<i32> for &'a T {
这里只能使用更高级别的约束,因为引用的生命周期比函数上任何可能的生命周期参数都短。
4🌟🌟🌟
4. 🌟🌟🌟
```rust
/* 添加 HRTB 使下面代码正常运行! */
fn call_on_ref_zero<'a, F>(f: F) where F: Fn(&'a i32) {
@ -188,7 +188,7 @@ fn main() {
```
5🌟🌟
5. 🌟🌟
```rust,editable
/* 通过重新排序一些代码使下面代码正常运行 */
fn main() {
@ -236,7 +236,7 @@ struct Ref<'a, T> {
## 艰难的练习
6🌟🌟🌟🌟
6. 🌟🌟🌟🌟
```rust
/* 使下面代码正常运行 */
struct Interface<'a> {

View File

@ -4,7 +4,7 @@
## 生命周期的范围
1 🌟
1. 🌟
```rust,editable
/* 为 `i``borrow2` 标注合适的生命周期范围 */
@ -106,7 +106,7 @@ fn main() {
}
```
3 🌟
3. 🌟
```rust,editable
/* 添加合适的生命周期标注,让下面的代码工作 */
fn longest(x: &str, y: &str) -> &str {
@ -119,7 +119,7 @@ fn longest(x: &str, y: &str) -> &str {
fn main() {}
```
4🌟🌟🌟
4. 🌟🌟🌟
```rust,editable
/* 使用三种方法修复下面的错误 */
fn invalid_output<'a>() -> &'a String {
@ -130,7 +130,7 @@ fn main() {
}
```
5🌟🌟
5. 🌟🌟
```rust,editable
// `print_refs` 有两个引用参数,它们的生命周期 `'a``'b` 至少得跟函数活得一样久
fn print_refs<'a, 'b>(x: &'a i32, y: &'b i32) {
@ -161,7 +161,7 @@ fn main() {
```
#### Structs
6 🌟
6. 🌟
```rust,editable
/* 增加合适的生命周期标准,让代码工作 */
@ -199,7 +199,7 @@ fn main() {
```
7 🌟🌟
7. 🌟🌟
```rust,editable
/* 让代码工作 */
@ -229,7 +229,7 @@ fn main()
```
8 🌟🌟
8. 🌟🌟
```rust,editable
#[derive(Debug)]
@ -277,7 +277,7 @@ fn main() {
}
```
9🌟🌟
9. 🌟🌟
```rust,editable
/* 添加合适的生命周期让下面代码工作 */
struct ImportantExcerpt {
@ -300,7 +300,7 @@ fn main() {}
这种规则被称为生命周期消除规则( Elision ),该规则之所以存在,仅仅是因为这些场景太通用了,为了方便用户而已。事实上对于借用检查器而言,该有的生命周期一个都不能少,只不过对于用户而言,可以省去一些。
10🌟🌟
10. 🌟🌟
```rust,editable
/* 移除所有可以消除的生命周期标注 */

View File

@ -13,7 +13,7 @@ fn generic<T>(x: T) where T: 'static {}
## &'static
作为一个引用生命周期,`&'static` 说明该引用指向的数据可以跟程序活得一样久,但是该引用的生命周期依然有可能被强转为一个更短的生命周期。
1🌟🌟 有好几种方法可以将一个变量标记为 `'static` 生命周期, 其中两种都是和保存在二进制文件中相关( 例如字符串字面量就是保存在二进制文件中,它的生命周期是 `'static` )。
1. 🌟🌟 有好几种方法可以将一个变量标记为 `'static` 生命周期, 其中两种都是和保存在二进制文件中相关( 例如字符串字面量就是保存在二进制文件中,它的生命周期是 `'static` )。
```rust,editable
@ -30,7 +30,7 @@ fn need_static(r : &'static str) {
}
```
2 🌟🌟🌟🌟 使用 `Box::leak` 也可以产生 `'static` 生命周期
2. 🌟🌟🌟🌟 使用 `Box::leak` 也可以产生 `'static` 生命周期
```rust,editable
#[derive(Debug)]
struct Config {
@ -57,7 +57,7 @@ fn main() {
}
```
3 🌟 `&'static` 只能说明引用指向的数据是能一直存活的,但是引用本身依然受限于它的作用域
3. 🌟 `&'static` 只能说明引用指向的数据是能一直存活的,但是引用本身依然受限于它的作用域
```rust,editable
fn main() {
{
@ -72,7 +72,7 @@ fn main() {
}
```
4 `&'static` 可以被强转成一个较短的生命周期
4. `&'static` 可以被强转成一个较短的生命周期
**Example**
```rust,editable
@ -103,7 +103,7 @@ fn main() {
关于 `'static` 的特征约束详细解释,请参见 [Rust 语言圣经](https://course.rs/advance/lifetime/static.html#t-static),这里就不再赘述。
5🌟🌟
5. 🌟🌟
```rust,editable
/* 让代码工作 */
use std::fmt::Debug;
@ -137,7 +137,7 @@ fn main() {
```
6🌟🌟🌟
6. 🌟🌟🌟
```rust,editable
use std::fmt::Display;