mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-22 20:19:42 +00:00
format code: format code in type-conversions/from-into
This commit is contained in:
@ -27,16 +27,15 @@ Some implementations of `From` trait can be found [here](https://doc.rust-lang.o
|
||||
|
||||
1. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
// impl From<bool> for i32
|
||||
let i1:i32 = false.into();
|
||||
let i2:i32 = i32::from(false);
|
||||
// impl From<bool> for i32
|
||||
let i1: i32 = false.into();
|
||||
let i2: i32 = i32::from(false);
|
||||
assert_eq!(i1, i2);
|
||||
assert_eq!(i1, 0);
|
||||
|
||||
// FIX the error in two ways
|
||||
/* 1. use a similar type which `impl From<char>`, maybe you
|
||||
/* 1. use a similar type which `impl From<char>`, maybe you
|
||||
should check the docs mentioned above to find the answer */
|
||||
// 2. a keyword from the last chapter
|
||||
let i3: i32 = 'a'.into();
|
||||
@ -51,7 +50,6 @@ fn main() {
|
||||
### Implement `From` for custom types
|
||||
2. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// From is now included in `std::prelude`, so there is no need to introduce it into the current scope
|
||||
// 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.
|
||||
```rust,editable
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::num;
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// impl From<bool> for i32
|
||||
let i1:i32 = false.into();
|
||||
let i2:i32 = i32::from(false);
|
||||
// impl From<bool> for i32
|
||||
let i1: i32 = false.into();
|
||||
let i2: i32 = i32::from(false);
|
||||
assert_eq!(i1, i2);
|
||||
assert_eq!(i1, 0);
|
||||
|
||||
@ -16,13 +16,13 @@ fn main() {
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// impl From<bool> for i32
|
||||
let i1:i32 = false.into();
|
||||
let i2:i32 = i32::from(false);
|
||||
// impl From<bool> for i32
|
||||
let i1: i32 = false.into();
|
||||
let i2: i32 = i32::from(false);
|
||||
assert_eq!(i1, i2);
|
||||
assert_eq!(i1, 0);
|
||||
|
||||
let i3: i32 = 'a' as i32 ;
|
||||
let i3: i32 = 'a' as i32;
|
||||
|
||||
let s: String = String::from('a');
|
||||
}
|
||||
|
@ -24,11 +24,10 @@ fn main() {
|
||||
|
||||
1. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
// impl From<bool> for i32
|
||||
let i1:i32 = false.into();
|
||||
let i2:i32 = i32::from(false);
|
||||
// impl From<bool> for i32
|
||||
let i1: i32 = false.into();
|
||||
let i2: i32 = i32::from(false);
|
||||
assert_eq!(i1, i2);
|
||||
assert_eq!(i1, 0);
|
||||
|
||||
@ -47,7 +46,6 @@ fn main() {
|
||||
### 为自定义类型实现 `From` 特征
|
||||
2. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// From 被包含在 `std::prelude` 中,因此我们没必要手动将其引入到当前作用域来
|
||||
// use std::convert::From;
|
||||
|
||||
@ -74,7 +72,6 @@ fn main() {
|
||||
|
||||
3. 🌟🌟🌟 当执行错误处理时,为我们自定义的错误类型实现 `From` 特征是非常有用。这样就可以通过 `?` 自动将某个错误类型转换成我们自定义的错误类型
|
||||
```rust,editable
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::num;
|
||||
|
Reference in New Issue
Block a user