[英]rustdoc no_main "crate-level attribute should be in the root module"
When using the no_main
outer attribute in Rust, cargo doc
complains about unused attribute
and that crate-level attribute should be in the root module
.在 Rust 中使用
no_main
外部属性时, cargo doc
抱怨unused attribute
,并且crate-level attribute should be in the root module
。
I am using Cargo 1.42.0-nightly.我每晚使用 Cargo 1.42.0。
Relevant code:相关代码:
// main.rs
#![no_main]
This only happens when invoking cargo doc
;这仅在调用
cargo doc
时发生; cargo build
works as expected. cargo build
按预期工作。
I have seen https://github.com/rust-lang/rust/issues/62184 , which however does not contain any answer.我看过https://github.com/rust-lang/rust/issues/62184 ,但是没有包含任何答案。 I have also seen https://github.com/rust-lang/rust/issues/43144 , from where I followed to https://github.com/rust-lang/rust/pull/64471 , but even though that merge request is already merged, the problem remains.
我也看过https://github.com/rust-lang/rust/issues/43144 ,从那里我跟随到https://github.com/rust-lang/rust/pull/64471 ,但即使合并请求已经合并,问题仍然存在。
How to solve this warning?如何解决这个警告?
This warning is caused by known bug in cargo/rustdoc which has however not yet been fixed.此警告是由货物/rustdoc 中的已知错误引起的,但尚未修复。
As of now, this can be easily worked around by using the cfg_attr
attribute in combination with the rustdoc's cfg(doc)
in the following way:到目前为止,可以通过以下方式将
cfg_attr
属性与 rustdoc 的cfg(doc)
结合使用来轻松解决此问题:
// main.rs
#![cfg_attr(not(doc), no_main)]
This code applies the no_main
attribute in all cases, except when the documentation is building.此代码在所有情况下都应用
no_main
属性,除非正在构建文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.