From d25f4c9c557bd88861b074a1f88c53de5917266d Mon Sep 17 00:00:00 2001 From: WangMengabc <98640292+WangMengabc@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:18:33 +0000 Subject: [PATCH] updata src/conversion/from_into.md (#198) * update src/cargo/conventions.md * updata src/conversion/from_into.md --- english/src/conversion/from_into.md | 10 +++++----- src/conversion/from_into.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) 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); }