Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I run a certain compiler flag the Rust compiler can't find my main

#![cfg(feature = "const_mut_refs")] //currently breaking my main function

fn main() {
    println!("Hello, world!");
}

Currently whenever I have this compiler flag on my nightly compiler my main function can’t be found. It gives me the error:
"main function not found in crate insert_name_here
consider adding a main function to src/main.rs"

I don’t know who to inform about said error, but I know it’s a nightly feature so I thought I should inform the community in some way. I tried to make the smallest reproducable version so others could tell me their experiences or if there was a way they could fix it.

I’d define this as a compiler error but not in the traditional sense, because the compiler is the one making the mistake.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

#![cfg(feature = "const_mut_refs")]

This does not enable the unstable const_mut_refs compiler feature.

  • The #[cfg] attribute is used for conditional compilation meaning it will disable code where the condition is not met.
  • The ! part means apply the attribute to the current module (in this case, the crate root).
  • The feature = "const_mut_refs" syntax will try looking for a cargo feature and not a compiler feature.

So effectively you are disabling your entire crate when the "const_mut_refs" cargo feature is not enabled, and thus main() is not found.

What you actually want is:

#![feature(const_mut_refs)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading