知识卡片
薄入口调用同包库
同一个包可以让 src/main.rs 只做启动和参数交接,把复用功能组织在 src/lib.rs 的库 crate 中。即使两者位于同一个包,二进制也以调用者身份通过库的公开接口使用功能。其他项目因此能重用核心逻辑,入口也能检验公开接口是否好用。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用包、crate 和模块管理增长中的项目 Starting Relative Paths with super · 查看原书依据
The module tree should be defined in src/lib.rs. Then, any public items can be used in the binary crate by starting paths with the name of the package. The binary crate becomes a user of the library crate just like a completely external crate would use the library crate: it can only use the public API.
参考来源
- 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0106)
- 结论依据:原文将二进制 crate 作为库的普通客户端,说明拆分如何约束依赖。
- 原始内容:The module tree should be defined in src/lib.rs. Then, any public items can be used in the binary crate by starting paths with the name of the package. The binary crate becomes a user of the library crate just like a completely external crate would use the library crate: it can only use the public API.