0

I like naming my folders and files by the kebab-case convention.

Recently, I've been learning rust and learned a bit about modules. In one of the examples, I have a file named: distinct-powers.rs. Whenever I do mod distinct-powers to put the code in scope, I obviously get a syntax error since Rust cannot handle kebab-case. The error is: Syntax Error: expected BANGrust-analyzer which I don't think is informing us of anything since it thinks the error is completely different.

Is there any way to circumvent this limitation of Rust?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
justbecause
  • 189
  • 2
  • 9

1 Answers1

10

If you really want to, you can place modules’ code at arbitrary filenames:

#[path = "distinct-powers.rs"]
mod distinct_powers;

But please don’t — one of the great things about Rust is that there is a single standard for project layout, which makes it easy to dive into other people’s code. Every customization is a disruption to someone finding the code they’re looking for.


There are a couple of locations where hyphens are allowed, that is, a kebab-case name can be used in a Rust package. They all have to do with Cargo, the build system, rather than the Rust language itself.

  • The name of the package itself (as specified in Cargo.toml) can be kebab-case. If the package is a library, Cargo will automatically translate this to snake_case for the library's crate name (which is how you refer to it within Rust code in dependents). This is quite commonly done.

    For example, in this docs URL, https://docs.rs/ordered-float/latest/ordered_float/, you can see that the package name is ordered-float and the crate name (which could be overridden but was not) is derived from that as ordered_float.

  • The name of a binary, example, or test target can contain hyphens. This means that the binaries built in target/ can have kebab-case names — cargo run --bin distinct-powers is valid.

These names can all be used by Cargo target auto-discovery and thus are not considered non-standard layout.

kebab-enthusiast/
  Cargo.toml
  src/
    lib.rs
    snake_module.rs
    bin/
      kebab-binary.rs
  examples/
    kebab-example.rs
  tests/
    kebab-test.rs
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • Thank you for your answer. I have one descending question if you don't mind. Do kebab-case programmers just not use the kebab-case convention in rust? I like this naming convention for exmple, what do people who like this convention do? Just not use it in Rust? – justbecause Oct 18 '22 at 20:59
  • 1
    @justbecause It is quite common to name Rust _packages_ (loosely called crates) with `kebab-case` names ([example](https://crates.io/crates/ordered-float)), and Cargo automatically translates their names into `snake_case` for use within the language. But for module organization within a package, the very strong convention is to stick to the precise standardized locations where the _files_ follow the _code_, not vice versa. – Kevin Reid Oct 18 '22 at 21:04
  • 1
    @justbecause I expanded my answer to cover more of this, with examples. – Kevin Reid Oct 18 '22 at 21:22
  • Thank you for your answer once again. I appreciate it. I will try not to use kebab-case as to keep in line with rust implied guidelines. – justbecause Oct 18 '22 at 21:22