知识卡片

普通读书笔记卡 · 10647.b · 生成模型:未记录

实例与方法各声明泛型

理解等级:中级

impl Point 的参数属于实现范围,所有匹配的 Point 实例都能用里面的方法。impl Point 则只为一种实例化提供额外方法。需要利用另一组类型时可在方法本身再声明参数,例如 mixup 填出 Point。

相关卡片 [[同名类型参数绑定同类]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 泛型类型、trait 与生命周期 In Struct Definitions · 查看原书依据

      The purpose of this example is to demonstrate a situation in which some generic parameters are declared with impl and some are declared with the method definition. Here, the generic parameters X1 and Y1 are declared after impl ❶ because they go with the struct definition. The generic parameters X2 and Y2 are declared after fn mixup ❷ because they’re only relevant to the method.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0094) - 结论依据:原文指出 mixup 方法泛型与 impl 泛型各自归属,解释哪些类型可以独立选择。 - 原始内容:The purpose of this example is to demonstrate a situation in which some generic parameters are declared with impl and some are declared with the method definition. Here, the generic parameters X1 and Y1 are declared after impl ❶ because they go with the struct definition. The generic parameters X2 and Y2 are declared after fn mixup ❷ because they’re only relevant to the method.