diff --git a/english/src/conversion/from_into.md b/english/src/conversion/from_into.md index 266d10f2..d927cdd7 100644 --- a/english/src/conversion/from_into.md +++ b/english/src/conversion/from_into.md @@ -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 for Number { - fn from(item: i32) -> Self { - Number { value: item } +impl Into 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); } diff --git a/src/conversion/from_into.md b/src/conversion/from_into.md index d6b0ea29..28ae2a38 100644 --- a/src/conversion/from_into.md +++ b/src/conversion/from_into.md @@ -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 for Number { - fn from(item: i32) -> Self { - Number { value: item } +impl Into for i32 { + fn into(self) -> Number { + Number { value: self } } } fn main() { let int = 5; - // 试试删除类型说明 + // 试试删除类型注释 let num: Number = int.into(); println!("My number is {:?}", num); }