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

Advertisements
#![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.

>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)]

Leave a ReplyCancel reply