我想记录我的箱子并在文档中包含一个表格:
//! Demonstrating MarkDown tables.
//!
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president | I can't think of any more "funny" things | oopsie |
//!
Run Code Online (Sandbox Code Playgroud)
渲染cargo doc
结果如下:
这就是我要的。但是,您可能已经注意到,源代码行非常长。事实上,长度超过100个字符。与许多 Rust 项目一样,我希望将所有行的长度控制在 100 个字符以内。所以我试图以某种方式打破界限。
所有这些版本:
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president
//! I can't think of any more "funny" things | oopsie |
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president |
//! I can't think of any more "funny" things | oopsie |
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president
//! | I can't think of any more "funny" things | oopsie |
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president |
//! | I can't think of any more "funny" things | oopsie |
//! | Foo | Bar | Baz | Qux |
//! | --- | --- | --- | --- |
//! | Hail the turbofish `::<>` | Ferris for president \
//! I can't think of any more "funny" things | oopsie |
Run Code Online (Sandbox Code Playgroud)
结果是:
我必须使用哪些选项才能在文档中包含长表行而不违反行长度限制?
使用 HTML 标记。
//! Demonstrating HTML tables.
//!
//! <table>
//! <thead>
//! <tr>
//! <th>Foo</th>
//! <th>Bar</th>
//! <th>Baz</th>
//! <th>Quux</th>
//! </tr>
//! </thead>
//! <tbody>
//! <tr>
//! <td>Hail the turbofish <code>::<></code></td>
//! <td>Ferris for president </td>
//! <td>
//! I can't think of any
//! more "funny" things
//! </td>
//! <td>oopsie</td>
//! </tr>
//! </tbody>
//! </table>
Run Code Online (Sandbox Code Playgroud)
正如弗朗西斯已经回答的那样,如果您想保持短行,则必须使用 HTML。rustdoc
使用pulldown-cmark并且不支持您想要的内容。
跟踪问题:rfc 1990 - 将外部 doc 属性添加到 rustc。对于nightly
工具链,您可以启用external_doc
功能并包含带有#[doc(include = "../some/path")]
.
需要注意的一件事 - 无论您将在哪个模块中使用#[doc(include = "...")]
,路径始终相对于 crate 根(lib.rs
,,main.rs
...)。
例子:
.
|____Cargo.toml
|____Cargo.lock
|____doc
| |____foo-bar-bar.md
|____src
| |____main-hallo.md
| |____foo
| | |____mod.rs
| | |____bar.rs
| |____main.rs
Run Code Online (Sandbox Code Playgroud)
src/main.rs
:
#![feature(external_doc)]
pub mod foo;
#[doc(include = "main-hallo.md")]
pub fn hallo() {}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
src/foo/bar.rs
:
#[doc(include = "../doc/foo-bar-bar.md")]
pub struct Bar;
Run Code Online (Sandbox Code Playgroud)
您可以在文件夹中保留单独的 Markdown 文档src/
,也可以将其放在单独的文件夹中doc/
,例如 等。但路径始终相对于 crate 根目录。
还有rdoc编译器插件(requires nightly
),它基本上做同样的事情。项目README.md中描述了如何启用和使用它。
为了稳定,我会执行以下操作:
build.rs
,它将扫描.md
文件并将其输出为.rs
文件(内容相同,只需在每一行前面加上///
或//!
),
std::env::var("OUT_DIR")
文件夹中,include!(concat!(env!("OUT_DIR"), "/main-hallo-md.rs"));
。有comment_width
选项(默认为80
)和wrap_comments
选项(默认为false
)。这可以帮助您将注释保持在一定的宽度内。但我用长 Markdown 表行尝试了它,它包裹了它 -> 损坏的表。不要使用它。