mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
@ -4,7 +4,7 @@ All types which want to be printable must implement the `std::fmt` formatting tr
|
|||||||
Automatic implementations are only provided for types such as in the `std` library. All others have to be manually implemented.
|
Automatic implementations are only provided for types such as in the `std` library. All others have to be manually implemented.
|
||||||
|
|
||||||
## Debug
|
## Debug
|
||||||
The implementation of `Debug` is very straightfoward: All types can `derive` the `std::fmt::Debug` implementation. This is not true for `std::fmt::Display` which must be manually implemented.
|
The implementation of `Debug` is very straightforward: All types can `derive` the `std::fmt::Debug` implementation. This is not true for `std::fmt::Display` which must be manually implemented.
|
||||||
|
|
||||||
`{:?}` must be used to print out the type which has implemented the `Debug` trait.
|
`{:?}` must be used to print out the type which has implemented the `Debug` trait.
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ Yeah, `Debug` is simple and easy to use. But sometimes we want to customize the
|
|||||||
|
|
||||||
Unlike `Debug`, there is no way to derive the implementation of the `Display` trait, we have to manually implement it.
|
Unlike `Debug`, there is no way to derive the implementation of the `Display` trait, we have to manually implement it.
|
||||||
|
|
||||||
Another thing to note: the placefolder for `Display` is `{}` not `{:?}`.
|
Another thing to note: the placeholder for `Display` is `{}` not `{:?}`.
|
||||||
|
|
||||||
4. 🌟🌟
|
4. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
@ -348,8 +348,8 @@ Returning a closure is much harder than you may have thought of.
|
|||||||
10. 🌟🌟
|
10. 🌟🌟
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Fill in the blank using two aproaches,
|
/* Fill in the blank using two approaches,
|
||||||
and fix the errror */
|
and fix the error */
|
||||||
fn create_fn() -> __ {
|
fn create_fn() -> __ {
|
||||||
let num = 5;
|
let num = 5;
|
||||||
|
|
||||||
|
@ -245,7 +245,7 @@ fn main() {
|
|||||||
10.
|
10.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
/* Fill in the blank and fix the errror */
|
/* Fill in the blank and fix the error */
|
||||||
// You can also use `impl FnOnce(i32) -> i32`
|
// You can also use `impl FnOnce(i32) -> i32`
|
||||||
fn create_fn() -> impl Fn(i32) -> i32 {
|
fn create_fn() -> impl Fn(i32) -> i32 {
|
||||||
let num = 5;
|
let num = 5;
|
||||||
@ -261,11 +261,11 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
/* Fill in the blank and fix the errror */
|
/* Fill in the blank and fix the error */
|
||||||
fn create_fn() -> Box<dyn Fn(i32) -> i32> {
|
fn create_fn() -> Box<dyn Fn(i32) -> i32> {
|
||||||
let num = 5;
|
let num = 5;
|
||||||
|
|
||||||
// how does the following closure capture the evironment variable `num`
|
// how does the following closure capture the environment variable `num`
|
||||||
// &T, &mut T, T ?
|
// &T, &mut T, T ?
|
||||||
Box::new(move |x| x + num)
|
Box::new(move |x| x + num)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user