Rust Rust Style Guide Naming


Example

Structs

// Structs use UpperCamelCase.
pub struct Snafucator {
    
}

mod snafucators {
    // Try to avoid 'stuttering' by repeating 
    // the module name in the struct name.

    // Bad:
    pub struct OrderedSnafucator {
    
    }
      
    // Good:
    pub struct Ordered {
        
    }
}

Traits

// Traits use the same naming principles as 
// structs (UpperCamelCase).
trait Read {
    fn read_to_snafucator(&self) -> Result<(), Error>;
}

Crates and Modules

// Modules and crates should both use snake_case.
// Crates should try to use single words if possible.
extern crate foo;
mod bar_baz {
    mod quux {

    }
}

Static Variables and Constants

// Statics and constants use SCREAMING_SNAKE_CASE.
const NAME: &'static str = "SCREAMING_SNAKE_CASE";

Enums

// Enum types and their variants **both** use UpperCamelCase.
pub enum Option<T> {
   Some(T),
   None
}

Functions and Methods

// Functions and methods use snake_case
fn snake_cased_function() {

}

Variable bindings

// Regular variables also use snake_case
let foo_bar = "snafu";

Lifetimes

// Lifetimes should consist of a single lower case letter. By 
// convention, you should start at 'a, then 'b, etc.

// Good:
struct Foobar<'a> {
    x: &'a str
}

// Bad:
struct Bazquux<'stringlife> {
    my_str: &'stringlife str
}

Acronyms

Variable names that contain acronyms, such as TCP should be styled as follows:

  • For UpperCamelCase names, the first letter should be capitalised (e.g. TcpClient)
  • For snake_case names, there should be no capitalisation (e.g. tcp_client)
  • For SCREAMING_SNAKE_CASE names, the acronym should be completely capitalised (e.g. TCP_CLIENT)