知识卡片

普通读书笔记卡 · 6076.a.1.a.2

公开结构不宜随功能增加必填项

理解等级:中级

公开可构造结构体若按feature增加必填字段,调用者可能不知道其他路径已启用该功能,初始化因缺字段失败。公开trait按feature新增必需方法也使外部实现者陷入同样问题。应稳定公共构造与实现契约,而不是要求下游猜测本crate最终功能集合。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 4. Dependencies Features · 查看原书依据

      ExposedStruct { pub data: Vec<u8>, /// Additional data that is required only when the `schema` feature /// is enabled. #[cfg(feature = "schema")] pub schema: String, } A user of the crate that tries to build an instance of the struct has a quandary: should they fill in the schema field or not? One way to try to solve this is to add a corresponding feature in

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第4章《Features》(spine8;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch04.html;u131-ch04-024)。 - 结论依据:原文在“ExposedStruct”附近的定义、例子或代码支撑本卡核心说明:公开可构造结构体若按feature增加必填字段,调用者可能不知道其他路径已启用该功能,初始化因缺字段失败。边界按已读前提解释;示例未执行。 - 原始内容:ExposedStruct { pub data: Vec<u8>, /// Additional data that is required only when the `schema` feature /// is enabled. #[cfg(feature = "schema")] pub schema: String, } A user of the crate that tries to build an instance of the struct has a quandary: should they fill in the schema field or not? One way to try to solve this is to add a corresponding feature in