Search code examples
rustnaming-conventionsrust-cargo

Can I apply a crate attribute only to the crate itself?


I have a Rust project that needs a few binaries with TitleCaseNames. So I created source files like src/bin/MyFooBinary.rs and src/bin/MyBarBinary.rs. The compiler warns that these binary crates should have snake case names, and I want to suppress that warning.

I can add #![allow(non_snake_case)] to the crate, but that then applies to the crate's entire contents as well. Not ideal. I really only want to suppress the warning for the crate name. Is that possible?


Solution

  • I'd suggest to follow the usual snake_name convention for the source file and crate names. Since you need to move the binaries elsewhere during deployment or packaging anyway, you can rename them at that point.

    In the nightly compiler build, you can also specify a filename that is different from the crate name in Cargo.toml:

    cargo-features = ["different-binary-name"]
    
    [package]
    # ...
    
    [[bin]]
    name = "foo_bar"
    filename = "FooBar"
    path = "src/bin/foo_bar.rs"