知识卡片

普通读书笔记卡 · 6074.a.2.b

实现From并约束Into

理解等级:中级

提供转换时通常实现目标类型的From<源类型>,标准库便能补出源类型的Into<目标类型>。泛型参数接收方通常写Into<目标类型>约束,再在函数体调用into;这样也容纳直接实现Into的类型。这是显式的接口设计,不是任意赋值位置的隐式转换。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types User-Defined Type Conversions · 查看原书依据

      implement the From trait for conversions. The Rust standard library had to pick just one of the two possibilities, in order to prevent the system from spiraling around in dizzy circles,12 and it came down on the side of automatically providing Into from a From implementation. If you’re consuming one of these two traits, as a trait bound on a new generic of y

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《User-Defined Type Conversions》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u032-ch01-040)。 - 结论依据:原文在“implement the From trait”附近的定义、例子或代码支撑本卡核心说明:提供转换时通常实现目标类型的From<源类型>,标准库便能补出源类型的Into<目标类型>。边界按已读前提解释;示例未执行。 - 原始内容:implement the From trait for conversions. The Rust standard library had to pick just one of the two possibilities, in order to prevent the system from spiraling around in dizzy circles,12 and it came down on the side of automatically providing Into from a From implementation. If you’re consuming one of these two traits, as a trait bound on a new generic of y