Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updata src/conversion/from_into.md #198

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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