Comments (5)
第一,如果 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.
得到和转化 不就是一个 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.
文档示例中,只展示了 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.
不好意思,好像我说的不够明确
上面已经贴出原文了
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.
综合所述,你的解释没问题,这一段有误区,我将修正这一段。
from rust-by-example-cn.
Related Issues (20)
- 3.1. 结构体 代码示例问题 HOT 1
- 链接可访问性 HOT 1
- 流程控制章节问题 HOT 1
- 18.4.4缺少dyn 关键词
- 函数章节问题
- 枚举那小节报错,多了个感叹号 HOT 2
- lang-ebook
- 单词拼写错误 HOT 1
- 建议改下网页里代码注释的字体样式 HOT 2
- 发现一些正文里边会多出一些空格,不确定是不是设计如此 HOT 4
- `指针和引用` 部分代码本地执行报错 HOT 1
- 常量和静态还是有区别的,静态并不能严格一定保证为常量。 HOT 1
- 想在公众号上 连载一下 中文翻译版本-仅用于学习,是否可以? HOT 1
- 5.4. 别名#[allow(non_camel_case_types)] HOT 2
- 9.2.6.1 Iterator::any对数组的into_iter()解构出也应该是i32 HOT 5
- 请求翻译rustwasm HOT 2
- rustwiki.org证书过期了
- Iterator::any, F: FnMut(Self::Item) -> bool {}
- 有没有动手试一试的代码式例
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-by-example-cn.