format code: format code in type-conversions/from-into

This commit is contained in:
chenhai
2023-08-06 22:30:24 +08:00
parent 88191e18b2
commit 9f2605bf6b
3 changed files with 14 additions and 20 deletions

View File

@ -27,11 +27,10 @@ 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();
let i2:i32 = i32::from(false); let i2: i32 = i32::from(false);
assert_eq!(i1, i2); assert_eq!(i1, i2);
assert_eq!(i1, 0); assert_eq!(i1, 0);
@ -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;

View File

@ -3,8 +3,8 @@
```rust ```rust
fn main() { fn main() {
// impl From<bool> for i32 // impl From<bool> for i32
let i1:i32 = false.into(); let i1: i32 = false.into();
let i2:i32 = i32::from(false); let i2: i32 = i32::from(false);
assert_eq!(i1, i2); assert_eq!(i1, i2);
assert_eq!(i1, 0); assert_eq!(i1, 0);
@ -17,12 +17,12 @@ fn main() {
```rust ```rust
fn main() { fn main() {
// impl From<bool> for i32 // impl From<bool> for i32
let i1:i32 = false.into(); let i1: i32 = false.into();
let i2:i32 = i32::from(false); let i2: i32 = i32::from(false);
assert_eq!(i1, i2); assert_eq!(i1, i2);
assert_eq!(i1, 0); assert_eq!(i1, 0);
let i3: i32 = 'a' as i32 ; let i3: i32 = 'a' as i32;
let s: String = String::from('a'); let s: String = String::from('a');
} }

View File

@ -24,11 +24,10 @@ 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();
let i2:i32 = i32::from(false); let i2: i32 = i32::from(false);
assert_eq!(i1, i2); assert_eq!(i1, i2);
assert_eq!(i1, 0); assert_eq!(i1, 0);
@ -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;