Rust Code Guidelines

Aug 08, 2021
Dec 20, 2023

Follow these standards to maintain consistency, readability, and best practices across our Rust projects.

Table of Contents

Naming Conventions

Formatting

fn main() {
    // Code goes here
}

Documentation

/// Adds two numbers together.
///
/// # Arguments
///
/// * `a` - The first number
/// * `b` - The second number
///
/// # Returns
///
/// The sum of the two numbers.
fn add(a: i32, b: i32) -> i32 {
    a + b
}

Error Handling

fn read_file(path: &str) -> Result<String, io::Error> {
    let content = fs::read_to_string(path)?;
    Ok(content)
}

Ownership and Borrowing

Pattern Matching

match some_result {
    Ok(value) => println!("Received: {}", value),
    Err(err) => eprintln!("Error: {}", err),
}

Concurrency

Testing

#[cfg(test)]
mod tests {
    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }
}

These guidelines aim to ensure code consistency and readability, fostering a collaborative and efficient development environment. Happy coding in Rust!