我如何告诉 cargo 在我的 README.md 文档中进行 运行 代码测试?

How can I tell cargo to run code test in documentation in my README.md?

我正在寻找一种方法将一些文档从 lib.rs 移动到 README.rs,以便它们出现在我的货箱上。我将文档移至 README.rs,并将其添加到 lib.rs

的顶部
#[doc(include="../README.md")]

但它会生成一个错误

use doc = include_str! instead: #[doc = include_str!("../README.md")] warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! note: for more information, see issue #82730 https://github.com/rust-lang/rust/issues/82730

然而,当我迁移到使用 #[doc = include_str!("../README.md")] 而不是 #[doc(include="../README.md")] 时,我收到警告,

warning: unknown doc attribute include_str

然后

warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! note: for more information, see issue #82730 https://github.com/rust-lang/rust/issues/82730

cargo test 测试我的 README.md 的正确语法是什么?

正确的语法是,

#[doc = include_str!("../README.md")]

在最新的 Rust 上,这将 不会 生成警告,但请注意,这不会单独显示在 cargo test.[=14= 中]

如果您的 ../README.md 中有一个失败的测试,您会看到它被报告为,

test src/lib.rs - sequence (line 22) ... FAILED

这似乎远非理想,因为文件名丢失并且行号不再相关。