Rust naming conventions
Created By: chatGPT
In Rust, naming conventions play a crucial role in code readability and consistency. Adopting the right conventions helps in making code more understandable for you and fellow developers. Below are the primary naming conventions used in Rust:
Variables and Function names should be in
snake_case
, where words are separated by underscores. For example:let my_variable = 10;
fn calculate_sum(a: i32, b: i32) -> i32 {
a + b
}
Constant names should be in
SCREAMING_SNAKE_CASE
, which means all uppercase letters with underscores separating words. An example of a constant would be:const MAX_RETRIES: u32 = 3;
Struct and Enum names should be in
CamelCase
, where each word starts with an uppercase letter. For instance:struct UserProfile {
username: String,
email: String,
}
enum Status {
Active,
Inactive,
}
Traits should also follow the
CamelCase
convention, similar to structs and enums. Here's an example:trait Drawable {
fn draw(&self);
}
For modules and crates, the convention is to use
snake_case
. This helps to distinguish them from types. Example:mod my_module {
pub fn new() {
// implementation
}
}
When naming type parameters, it is commonplace to use single uppercase letters, such as
By adhering to these conventions, you'll contribute to a cleaner, more maintainable, and more professional codebase in Rust.T
, U
, or V
.fn generic_function<T>(value: T) {
// do something
}