codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

55 / 58 topics
54Best Practices55Code Style56Performance Tips
Tutorials/Rust/Code Style
🦀Rust

Code Style

Updated 2026-04-20
3 min read

Introduction

Code style is a crucial aspect of software development, influencing code readability, maintainability, and collaboration among developers. In Rust, adhering to consistent code style guidelines not only enhances the quality of your code but also aligns with the language's philosophy of safety and performance.

This tutorial will cover best practices for writing clean, idiomatic Rust code. We'll explore various aspects of code style, including naming conventions, formatting, and structuring, providing real-world examples to illustrate each point.

Naming Conventions

Proper naming is essential for making your code self-explanatory. Rust follows a set of conventions that help maintain consistency across the language ecosystem.

Variables and Functions

  • Use snake_case: Variable names should be in lowercase with words separated by underscores.

    let user_name = "Alice";
    fn calculate_area(width: u32, height: u32) -> u32 {
        width * height
    }
    
  • Descriptive Names: Choose meaningful names that convey the purpose of the variable or function.

    // Bad
    let x = 10;
    fn y(a: i32, b: i32) -> i32 {
        a + b
    }
    
    // Good
    let max_users = 10;
    fn sum_values(first_value: i32, second_value: i32) -> i32 {
        first_value + second_value
    }
    

Types and Enums

  • Use CamelCase for Types: Structs, enums, and other types should be in camelCase.

    struct UserProfile {
        name: String,
        age: u8,
    }
    
    enum Status {
        Active,
        Inactive,
    }
    
  • Acronyms Should Be Uppercase: If a type name includes acronyms, they should be in uppercase.

    struct HTTPRequest {
        method: String,
        url: String,
    }
    

Constants and Statics

  • Use SCREAMING_SNAKE_CASE for Constants: Constants should have all letters in uppercase with underscores separating words.
    const MAX_CONNECTIONS: u32 = 100;
    static APP_VERSION: &str = "1.0";
    

Formatting

Rust provides tools like rustfmt to automatically format code according to a set of style guidelines. While it's beneficial to use these tools, understanding the underlying principles is crucial.

Indentation and Braces

  • Use Four Spaces for Indentation: Rust recommends using four spaces per indentation level.

    fn main() {
        if true {
            println!("Hello, world!");
        }
    }
    
  • Consistent Brace Placement: Opening braces should be on the same line as the statement or expression they belong to.

    // Bad
    fn main()
    {
        println!("Hello, world!");
    }
    
    // Good
    fn main() {
        println!("Hello, world!");
    }
    

Line Length

  • Limit Lines to 80 Characters: While not strictly enforced by rustfmt, keeping lines under 80 characters improves readability.
    // Bad
    let long_variable_name = "This is a very long string that should be split into multiple lines";
    
    // Good
    let long_variable_name = "This is a very long \
                              string that should be split \
                              into multiple lines";
    

Structuring Code

Proper structuring helps in organizing code logically and making it easier to navigate.

Modules and Crates

  • Organize Code by Functionality: Use modules to group related functions, structs, and enums.

    // src/lib.rs
    mod user;
    mod utils;
    
    pub use user::UserProfile;
    pub use utils::calculate_area;
    
  • Keep Crates Small and Focused: Each crate should have a single responsibility. This makes them easier to maintain and reuse.

Comments

  • Use Doc Comments for Public APIs: Document public functions, structs, and enums using doc comments.

    /// Represents a user profile in the application.
    pub struct UserProfile {
        name: String,
        age: u8,
    }
    
  • Inline Comments for Complex Logic: Use inline comments to explain complex logic or non-obvious code patterns.

    // Calculate the factorial of a number using recursion
    fn factorial(n: u32) -> u32 {
        if n == 0 {
            1
        } else {
            n * factorial(n - 1)
        }
    }
    

Best Practices

Avoid Redundant Code

  • Use Traits for Common Functionality: Define traits to encapsulate common behavior and reuse code.
    trait Printable {
        fn print(&self);
    }
    
    impl Printable for UserProfile {
        fn print(&self) {
            println!("Name: {}, Age: {}", self.name, self.age);
        }
    }
    

Handle Errors Gracefully

  • Use Result and Option Types: Prefer using these types to handle errors and missing values instead of panicking.
    fn read_file(path: &str) -> Result<String, std::io::Error> {
        std::fs::read_to_string(path)
    }
    

Write Tests

  • Test Your Code: Write unit tests for your functions to ensure they work as expected.
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        fn test_calculate_area() {
            assert_eq!(calculate_area(4, 5), 20);
        }
    }
    

Conclusion

Adhering to a consistent code style is vital for writing high-quality Rust code. By following the best practices outlined in this tutorial, you can ensure that your code is not only functional but also easy to read and maintain.

Remember, while tools like rustfmt can automate much of the formatting process, understanding the underlying principles will help you write cleaner, more idiomatic Rust code.


PreviousBest PracticesNext Performance Tips

Recommended Gear

Best PracticesPerformance Tips