Skip to content

Commit

Permalink
updata src/conversion/from_into.md (#198)
Browse files Browse the repository at this point in the history
* update src/cargo/conventions.md

* updata src/conversion/from_into.md
  • Loading branch information
WangMengabc authored Sep 11, 2024
1 parent 0cb07db commit d25f4c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions english/src/conversion/from_into.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ convert into as the compiler is unable to determine this most of the time.
However this is a small trade-off considering we get the functionality for free.

```rust,editable
use std::convert::From;
use std::convert::Into;
#[derive(Debug)]
struct Number {
value: i32,
}
impl From<i32> for Number {
fn from(item: i32) -> Self {
Number { value: item }
impl Into<Number> for i32 {
fn into(self) -> Number {
Number { value: self }
}
}
fn main() {
let int = 5;
// Try removing the type declaration
// Try removing the type annotation
let num: Number = int.into();
println!("My number is {:?}", num);
}
Expand Down
10 changes: 5 additions & 5 deletions src/conversion/from_into.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ fn main() {
使用 `Into` trait 通常要求指明要转换到的类型,因为编译器大多数时候不能推断它。不过考虑到我们免费获得了 `Into`,这点代价不值一提。

```rust,editable
use std::convert::From;
use std::convert::Into;
#[derive(Debug)]
struct Number {
value: i32,
}
impl From<i32> for Number {
fn from(item: i32) -> Self {
Number { value: item }
impl Into<Number> for i32 {
fn into(self) -> Number {
Number { value: self }
}
}
fn main() {
let int = 5;
// 试试删除类型说明
// 试试删除类型注释
let num: Number = int.into();
println!("My number is {:?}", num);
}
Expand Down

0 comments on commit d25f4c9

Please sign in to comment.