当我运行 cargo doc
,它似乎没有为我的项目中的库生成文档。我正在使用最新版本的 Rust,正如这篇文章所回答的那样:How can I include private modules when generating documentation via Cargo?
这是我的结构:
- services/
- mod.rs
- my_service.rs
- lib.rs
- main.rs
main.rs
仅包含要启动的“主要”功能:use test_doc::Core;
fn main() {
Core::start();
}
lib.rs
包含实际逻辑:mod services;
/// Core process
pub struct Core;
impl Core {
pub fn start() -> ! {
loop {
// do stuff here
}
}
}
然后my_service.rs
包含更多逻辑:/// My service should do stuff
pub struct MyService;
impl MyService {
/// This function actually does stuff
pub fn do_stuff(&self) -> &'static str {
"doing stuff"
}
}
mod.rs
里面 my_service
文件夹只是作为一个入口点:pub mod my_service;
此代码编译并成功执行,但我不确定为什么文档没有正确生成。这是我运行
cargo doc --open
时生成的文档的屏幕截图:我找不到
MyService
记录在任何地方...(单击“结构”链接只会跳转到主页上的 anchor )
最佳答案
一个小得多的例子:
src/main.rs
//! THE BINARY
fn main() {}
src/lib.rs //! THE LIBRARY
/// You can't see me
fn private() {}
当我运行 cargo doc
,我看到 macOS 上的 Rust 1.55 生成了该库的文档。如 How can I include private modules when generating documentation via Cargo? 中所述,记录图书馆时,不包括私有(private)项目。您需要通过 --document-private-items
旗帜看到他们。如果你想记录二进制文件,你需要传递
--bin
或 --bins
旗帜。如果你想记录图书馆,你需要通过 --lib
旗帜。
关于rust - Cargo doc 不会为既是库又是二进制的项目中的私有(private)项目生成文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68728262/