mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
format code: format code in type-conversions/from-into
This commit is contained in:
@ -27,7 +27,6 @@ Some implementations of `From` trait can be found [here](https://doc.rust-lang.o
|
|||||||
|
|
||||||
1. 🌟🌟🌟
|
1. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// impl From<bool> for i32
|
// impl From<bool> for i32
|
||||||
let i1: i32 = false.into();
|
let i1: i32 = false.into();
|
||||||
@ -51,7 +50,6 @@ fn main() {
|
|||||||
### Implement `From` for custom types
|
### Implement `From` for custom types
|
||||||
2. 🌟🌟
|
2. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// From is now included in `std::prelude`, so there is no need to introduce it into the current scope
|
// From is now included in `std::prelude`, so there is no need to introduce it into the current scope
|
||||||
// use std::convert::From;
|
// use std::convert::From;
|
||||||
|
|
||||||
@ -78,7 +76,6 @@ fn main() {
|
|||||||
|
|
||||||
3. 🌟🌟🌟 When performing error handling it is often useful to implement `From` trait for our own error type. Then we can use `?` to automatically convert the underlying error type to our own error type.
|
3. 🌟🌟🌟 When performing error handling it is often useful to implement `From` trait for our own error type. Then we can use `?` to automatically convert the underlying error type to our own error type.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::num;
|
use std::num;
|
||||||
|
@ -24,7 +24,6 @@ fn main() {
|
|||||||
|
|
||||||
1. 🌟🌟🌟
|
1. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// impl From<bool> for i32
|
// impl From<bool> for i32
|
||||||
let i1: i32 = false.into();
|
let i1: i32 = false.into();
|
||||||
@ -47,7 +46,6 @@ fn main() {
|
|||||||
### 为自定义类型实现 `From` 特征
|
### 为自定义类型实现 `From` 特征
|
||||||
2. 🌟🌟
|
2. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// From 被包含在 `std::prelude` 中,因此我们没必要手动将其引入到当前作用域来
|
// From 被包含在 `std::prelude` 中,因此我们没必要手动将其引入到当前作用域来
|
||||||
// use std::convert::From;
|
// use std::convert::From;
|
||||||
|
|
||||||
@ -74,7 +72,6 @@ fn main() {
|
|||||||
|
|
||||||
3. 🌟🌟🌟 当执行错误处理时,为我们自定义的错误类型实现 `From` 特征是非常有用。这样就可以通过 `?` 自动将某个错误类型转换成我们自定义的错误类型
|
3. 🌟🌟🌟 当执行错误处理时,为我们自定义的错误类型实现 `From` 特征是非常有用。这样就可以通过 `?` 自动将某个错误类型转换成我们自定义的错误类型
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::num;
|
use std::num;
|
||||||
|
Reference in New Issue
Block a user