知识卡片
重导出让用户取得匹配类型
公共接口既然使用依赖类型,可重导出相关类型或依赖库,使用户通过dep_lib::rand构造接口所需版本,减少隐蔽错配。它改善可访问性,但没有消除版本耦合。原书“二进制不能直接用两版”过于绝对,Cargo可通过package重命名依赖同时引用不同版本。
补充依据:Cargo重命名依赖,2026-10-27查阅。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 4. Dependencies Item 24: Re-export dependencies whose types appear in your API · 查看原书依据
pub use rand; The calling code now has a different way to directly refer to version 0.7 of rand, as dep_lib::rand: let mut prev_rng = dep_lib::rand::thread_rng(); // v0.7 Rng instance let choice = dep_lib::pick_number_with(&mut prev_rng, max); With this example in mind, the advice given in the title of the Item should now be a little less obscure: re-export
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第4章《Item 24: Re-export dependencies whose types appear in your API》(spine8;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch04.html;u123-ch04-012)。
- 结论依据:原文在“pub use rand”附近的定义、例子或代码支撑本卡核心说明:公共接口既然使用依赖类型,可重导出相关类型或依赖库,使用户通过dep_lib::rand构造接口所需版本,减少隐蔽错配。边界按已读前提解释;示例未执行。
- 原始内容:pub use rand; The calling code now has a different way to directly refer to version 0.7 of rand, as dep_lib::rand: let mut prev_rng = dep_lib::rand::thread_rng(); // v0.7 Rng instance let choice = dep_lib::pick_number_with(&mut prev_rng, max); With this example in mind, the advice given in the title of the Item should now be a little less obscure: re-export