Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning function should have a snake case identifier on by default

Tags:

rust

I'm trying to figure out what this warning actually means. The program works perfectly but during compile I get this warning:

main.rs:6:1: 8:2 warning: function 'isMultiple' should have a snake case identifier, 
#[warn(non_snake_case_functions)] on by default

the code is very simple:

/*
    Find the sum of all multiples of 3 or 5 below 1000
*/


fn isMultiple(num: int) -> bool {
    num % 5 == 0 || num % 3 == 0
}

fn main() {
    let mut sum_of_multiples = 0; 

    //loop from 0..999
    for i in range(0,1000) {
        sum_of_multiples += 
            if isMultiple(i) {
                i
            }else{
                0
            };
    }
    println!("Sum is {}", sum_of_multiples);

}
like image 399
Syntactic Fructose Avatar asked Jun 08 '14 01:06

Syntactic Fructose


3 Answers

You can turn it off by including this line in your file. Check out this thread

#![allow(non_snake_case)]
like image 170
nv.snow Avatar answered Nov 13 '22 05:11

nv.snow


Rust style is for functions with snake_case names, i.e. the compiler is recommending you write fn is_multiple(...).

like image 9
huon Avatar answered Nov 13 '22 05:11

huon


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.

-ZiCog

Link to thread : https://users.rust-lang.org/t/is-snake-case-better-than-camelcase-when-writing-rust-code/36277/2

like image 1
Aditya Avatar answered Nov 13 '22 07:11

Aditya