Giter VIP home page Giter VIP logo

Comments (5)

YangFong avatar YangFong commented on June 25, 2024

第一,如果 A --> B,那么可以相信 B --> A,这表示转换操作可逆,没有问题。
第二,目前该段的最新原文如下,核心意思没有翻译问题:

If you are able to convert type A from type B, then it should be easy to believe that we should be able to convert type B to type A.

from rust-by-example-cn.

katteXu avatar katteXu commented on June 25, 2024

得到和转化 不就是一个 from 和一个 into 吗?
翻译成代码的话 不是应该这样吗?

struct Number {
    value: i32,
}

impl From<i32> for Number {
    fn from(value: i32) -> Self {
        Number { value }
    }
}


fn main (){
    // 从 10 (A) 得到 Number (B)
    let number = Number::from(10);
    // 将 10 (A) 转化成 Number (B)
    let number2:Number = 10.into();
}

翻译成代码 如何从 B 到 A 呢?

from rust-by-example-cn.

YangFong avatar YangFong commented on June 25, 2024

文档示例中,只展示了 A --> B,若是想要 B --> A,补充对应的实现即可。

struct Number {
    value: i32,
}

impl From<i32> for Number {
    fn from(value: i32) -> Self {
        Number { value }
    }
}

impl From<Number> for i32 {
    fn from(number: Number) -> Self {
        number.value
    }
}

fn main() {
    // 从 i32 得到 Number
    let number = Number::from(10);
    // 将 Number 转换为 i32
    let i = i32::from(number);
    println!("{}", i); // 输出 10
}

from rust-by-example-cn.

katteXu avatar katteXu commented on June 25, 2024

不好意思,好像我说的不够明确

上面已经贴出原文了
If you are able to convert type A from type B, then it should be easy to believe that we should be able to convert type B to type A.

convert type A from type B 我理解应该是 从B转化成A 即 B --> A
convert type B to A 也是B转化成A 即B --> A
并不是可逆操作吧

所以我提出要改正的 应该是:
如果我们能够从类型 B 得到类型 A,那么很容易相信我们也能把类型 B 转换为类型 A。

这样是否能够理解呢?

from rust-by-example-cn.

YangFong avatar YangFong commented on June 25, 2024

综合所述,你的解释没问题,这一段有误区,我将修正这一段。

from rust-by-example-cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.