Is snake_case better than camelCase when writing rust code?

I got this warning on my compiler suggesting snake case when i use camel case, i was just wondering why does that have to be an issue with rust?

It's a convention with the purpose of having rust code look similar to other rust code.

3 Likes

My take on it is that programmers have been debating the case of names and other formatting over and over and over again, for decades. Everyone has their preferences, they are all different.

It's time we all grew up and realized that it's better we all use the same case and formatting. That makes code much easier to read for everyone in the long run. It's time to quit the selfish bickering over preferences. Just do as everyone else.

Personally I'm not much into underscores so the Rust standard upset me a bit. But I'm prepared to get use to it. Wouldn't it be great if we all did that and never had to waste time thinking and arguing about it again.

To that end I use cargo-fmt and clippy and I accept whatever they say to do. Job done, move on to next thing.

There is a method in the Rust convention:

Structs get camel case.
Variables get snake case.
Constants get all upper case.

Makes it easy to see what is what at a glance.

14 Likes

This reminds me of one of my favourite Go proverbs:

Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.

I personally find snake_case easier to read than camelCase, but at work we've got a large codebase (hundreds of kloc) written in C# and I use camelCase because that's the C# convention.

Sometimes it's easier to realise there is no "best" way of doing things, so just adopt the convention used by your language/project, set up tooling to enforce style, then move onto more productive things.

6 Likes

Yes. Go with the "house" style. Keep the house happy. Every company/project/team has it's rules.

I like to think that in this time of global open source development we are all in the same team working on the same code base that we share with each other. No matter who you are or where you are.

Ergo we should all adopt the Rust house style and be done with discussing it. Even if it is not what we might have chosen for ourselves.

I'm lazy anyway. I just do what the compiler, rust-fmt and clippy tell me. Saves me the trouble of having to think about it and maintain my own style rules.

1 Like

Because the recommended style uses different conventions for type/variant names, variables, and constants, it also helps the compiler give better feedback when you make certain errors like mis-spelling a variant name in a match expression.

(If variables and variants used the same style, then it would be less clear whether you intended to write a variant name or create a new variable.)

12 Likes

Is there a document I can read to understand all the conventions and writing style for Rust code?

Or better still if you can post them here.

Thanks.

2 Likes

This is the manual I'm using: https://doc.rust-lang.org/1.0.0/style/style/naming/README.html

2 Likes

There's also the rustfmt style guide. It doesn't go much into the "why" of the rules but it does have some very general guiding principles.

You can use rustfmt to automatically style your code according to these rules, if you want.

2 Likes

Ok, great thanks alot.

I have never seen any style guide.

I just have a conversation with "cargo clippy".

Clippy tell me what it does not like and makes suggestions as to how to fix it.

1 Like

Who is this clippy you always talk about? :thinking:

It's a tool that judges your code and prints thousands of warnings for your project: link.

3 Likes

This guide is out of date; see the 1.0.0 in the URL and the

in the introduction.

The rustfmt guide is good, there are also several RFCs that it incorporates. See

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.