Coding/Naming convention

Do we have a coding convention/guideline for rust programs? Getting names right is hard but in my opinion it is of utmost importance. The concept of abstraction depends on using a good name as a mnemonic/handle to a larger concept. Also mistakes in choosing good names are hard to rectify down the line.

I propose the following rules to begin with the process of creating such a document (assuming it does not exist)...

  1. Struct names should be nouns.
  2. Trait names should be adjectives.
  3. Method names should be verbs.

I'm not sure if there are any guidelines on that currently.

I know that in the new io module though, all of the structs seem to have been named nouns, the traits have been named verbs, and the methods have been named verbs or plural nouns.

For example:

  • Write, the trait for something to write to - similarly BufWrite, the trait for something that writes in a buffered way
  • BufWriter, an implementation of BufWrite.
  • Write::write, a method to write something to the Write.
  • or in a different method name case, Read::chars, a method to produce a character iterator from a Read.

But, in another case, structs have also been called verbs. The ones in the io trait using this convention are all iterators, which take the name directly from the methods which produce them.

For example:

  • Using io::repeat(u8) will produce a Repeat struct
  • Using BufReader::split(self, u8) will produce a Split struct

Besides those trends, I don't know of any specific guidelines. I think the general consensus is just to go with something which will give the user an idea of what the function will do, and not to have it in any specific part of speech.

This WIP guide is probably what you're looking for. Certain things do have naming conventions but it depends. I don't know of anything dictating verbs vs nouns and such.

[EDITED] Apparently the original guide was moved to here.

is it a good practice to use Naming convention like

no Type Suggestions
1 Variable name snake_case and lower case
2 Traits or class name: Camelcase and start with uppercase
3 Function name Camelcase and start with lowercase
4 struct/enum name start with UPPERCASE
5 Constant name snake_case and UPPERCASE
6 Library/Bin/module name lowercase

in RustLang

in many case rust compiler output warning #[warn(non_snake_case)] if we name lib or bin name in uppercase

if any one have better suggestions or corrections please share

  1. It's very bad practice to revive the several-years-old threads instead of creating new ones (with link to the original thread, if it is necessary for context).
  2. In general, it's good practice to follow the common code style, no matter the language, so that your code can be understood by someone else but you. For Rust, this is the style enforced by compiler lints. But if you don't want your code to be easily read by others (only by yourself), then, well, you may use whatever style you want, and there would be no "bad" or "good" practice in any case.
4 Likes

I am confused about right naming convention because different language follow different naming convention for example c#/java have different naming convention when compare to python can anyone explain right naming convention or coding formate for rust language that is officially accepted by rust community

First link: Lint groups chapter of rustc book. Quoting:

nonstandard-style: Violation of standard naming conventions; lints: non-camel-case-types, non-snake-case, non-upper-case-globals

Second link: Warn-by-default lints from the same book, where you can find the lints listed above.

Third - well, just write the code so that you don't have any warnings and don't explicitly #[allow(something)]. In most cases that'll be enough.

No need to be confused. Or even think about it much. Just edit and compile your code fixing things up as per the suggestions in the compiler warnings until all the yellow squiggles have gone away.

There is a logic to the Rust naming convention which you will soon absorb.

Whilst you are at it use "cargo clippy" and "cargo fmt". With their default settings.

If everyone does that we will end up a happy world where all code you come across is named and formatted consistently.