知识卡片

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

重导出塑造公共接口

理解等级:中级

用 pub use 把一个定义重导出到用户容易找到的位置,内部可保留按职责划分的模块。例如 art::mix 可指向 utils 里的函数。如果继续保留原先的公开模块,调用者仍可依赖旧路径。重导出的外部依赖也构成公开契约。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 深入 Cargo 与 Crates.io Exporting a Convenient Public API with pub use · 查看原书依据

      The good news is that if the structure isn’t convenient for others to use from another library, you don’t have to rearrange your internal organization: instead, you can re-export items to make a public structure that’s different from your private structure by using pub use. Re-exporting takes a public item in one location and makes it public in another location, as if it were defined in the other location instead.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第14章 深入 Cargo 与 Crates.io(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c14.xhtml`,块 0062) - 结论依据:原文表明 `pub use` 能创建公开路径而无需搬动定义。 - 原始内容:The good news is that if the structure isn’t convenient for others to use from another library, you don’t have to rearrange your internal organization: instead, you can re-export items to make a public structure that’s different from your private structure by using pub use. Re-exporting takes a public item in one location and makes it public in another location, as if it were defined in the other location instead.