mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
fix: change 、to .
This commit is contained in:
@ -41,7 +41,7 @@ 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 changing */
|
||||
fn main() {
|
||||
@ -60,7 +60,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
2、🌟🌟
|
||||
2. 🌟🌟
|
||||
```rust,editable
|
||||
/* Make it work
|
||||
- Dont use `_reborrow` and `_count_reborrowed`
|
||||
@ -89,7 +89,7 @@ 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 +132,7 @@ 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 +151,7 @@ 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 change the trait bound, in two ways*/
|
||||
fn fn_once<F>(func: F)
|
||||
@ -168,7 +168,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
6、 🌟🌟
|
||||
6. 🌟🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let mut s = String::new();
|
||||
@ -198,7 +198,7 @@ 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 +285,7 @@ fn exec<F: Fn()>(f: F) {
|
||||
}
|
||||
```
|
||||
|
||||
8、🌟🌟
|
||||
8. 🌟🌟
|
||||
```rust,editable
|
||||
/* Fill in the blank */
|
||||
fn main() {
|
||||
@ -305,7 +305,7 @@ fn exec<'a, F: __>(mut f: F) {
|
||||
## Input functions
|
||||
Since closure maybe used as arguments, you might wonder can we use functions as arguments too? And indeed they can.
|
||||
|
||||
9、🌟🌟
|
||||
9. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
/* Implement `call_me` to make it work */
|
||||
@ -328,7 +328,7 @@ fn main() {
|
||||
## Closure as return types
|
||||
Returning a closure is much harder than you may thought of.
|
||||
|
||||
10、🌟🌟
|
||||
10. 🌟🌟
|
||||
```rust,editable
|
||||
/* Fill in the blank using two approches,
|
||||
and fix the errror */
|
||||
@ -347,7 +347,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
11、🌟🌟
|
||||
11. 🌟🌟
|
||||
```rust,editable
|
||||
/* Fill in the blank and fix the error*/
|
||||
fn factory(x:i32) -> __ {
|
||||
|
@ -23,7 +23,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
1、🌟
|
||||
1. 🌟
|
||||
```rust,editable
|
||||
/* Refactoring the following code using iterators */
|
||||
fn main() {
|
||||
@ -34,7 +34,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
2、 🌟 One of the easiest ways to create an iterator is to use the range notion: `a..b`.
|
||||
2. 🌟 One of the easiest ways to create an iterator is to use the range notion: `a..b`.
|
||||
```rust,editable
|
||||
/* Fill in the blank */
|
||||
fn main() {
|
||||
@ -61,7 +61,7 @@ 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 +83,7 @@ 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 +96,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
5、🌟
|
||||
5. 🌟
|
||||
```rust,editable
|
||||
/* Fill in the blank */
|
||||
fn main() {
|
||||
@ -113,7 +113,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
6、🌟🌟
|
||||
6. 🌟🌟
|
||||
```rust,editable
|
||||
/* Fill in the blank */
|
||||
fn main() {
|
||||
@ -169,7 +169,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
7、🌟🌟🌟
|
||||
7. 🌟🌟🌟
|
||||
```rust,editable
|
||||
struct Fibonacci {
|
||||
curr: u32,
|
||||
@ -208,7 +208,7 @@ 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、🌟🌟
|
||||
8. 🌟🌟
|
||||
```rust,edtiable
|
||||
/* Fill in the blank and fix the errors */
|
||||
fn main() {
|
||||
@ -229,7 +229,7 @@ fn main() {
|
||||
#### collect
|
||||
Other than converting a collection into an iterator, we can also `collect` the result values into a collection, `collect` will cosume the iterator.
|
||||
|
||||
9、🌟🌟
|
||||
9. 🌟🌟
|
||||
```rust,editable
|
||||
/* Make it work */
|
||||
use std::collections::HashMap;
|
||||
@ -253,7 +253,7 @@ 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 adapers to get results from calls to iterator adapters.
|
||||
|
||||
10、🌟🌟
|
||||
10. 🌟🌟
|
||||
```rust,editable
|
||||
/* Fill in the blanks */
|
||||
fn main() {
|
||||
@ -265,7 +265,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
11、🌟🌟
|
||||
11. 🌟🌟
|
||||
```rust
|
||||
/* Fill in the blanks */
|
||||
use std::collections::HashMap;
|
||||
@ -281,7 +281,7 @@ fn main() {
|
||||
|
||||
#### Using closures in iterator adaptors
|
||||
|
||||
12、🌟🌟
|
||||
12. 🌟🌟
|
||||
```rust
|
||||
/* Fill in the blanks */
|
||||
#[derive(PartialEq, Debug)]
|
||||
|
@ -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 */
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user