William Stanley
William Stanley

Reputation: 751

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.

I am using Cargo 1.42.0-nightly.

Relevant code:

// main.rs

#![no_main]

This only happens when invoking cargo doc; cargo build works as expected.

I have seen https://github.com/rust-lang/rust/issues/62184, which however does not contain any answer. 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.

How to solve this warning?

Upvotes: 2

Views: 2081

Answers (1)

William Stanley
William Stanley

Reputation: 751

This warning is caused by known bug in cargo/rustdoc which has however not yet been fixed.

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:

// main.rs

#![cfg_attr(not(doc), no_main)]

This code applies the no_main attribute in all cases, except when the documentation is building.

Upvotes: 3

Related Questions