Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.3 Functions, Control Flow, Comments

ℹ️ Rust coding rules suggestions:

Item TypeCase StyleExample
Project Namesnake_case or kebab-casemy_project or my-project
Variablessnake_casemy_variable
Functionssnake_casecalculate_sum()
StructsPascalCaseMyStruct
EnumsPascalCaseMyEnum
Enum VariantsPascalCaseSomeVariant
ConstantsSCREAMING_SNAKE_CASEMAX_LIMIT
StaticsSCREAMING_SNAKE_CASEMAX_LIMIT
Modulessnake_casemy_module
Cratessnake_casemy_crate
TraitsPascalCaseDisplay
Type AliasesPascalCaseMyType
Lifetimeslowercase with ''a, 'static

Functions

Function definition syntax of Rust

fn function_name(parameter1: Type1, parameter2: Type2, ...) -> ReturnType {
    // function body
}
  • The return type comes after the arrow ->.
  • If the last expression in the function block is returned implicitly, you omit the semicolon.
  • If you use return, you must end the line with a semicolon.

When a generic type has to be used in the function definition

  1. Basic generic function:

    fn identity<T>(value: T) -> T {
        value
    }
  2. Generic with constraints (trait bounds)

    fn print_value<T: std::fmt::Display>(value: T) {
        println!("{}", value);
    }
  3. Multiple generic parameters

     fn pair<T, U>(a: T, b: U) {
        // do something
    }
  4. With return type and trait bounds

    fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
        a + b
    }
  5. Syntax with "where" keyword

    fn function_name<T, U>(param1: T, param2: U) -> ReturnType
    where
        T: Trait1 + Trait2,
        U: Trait3,
    {
        // function body
    }

Control Flows

Conditional statements

SyntaxDescription
if / else if / elseBranches execution based on a condition
matchPattern matching control flow
fn main() {
    if x > 0 {
        println!("Positive");
    } else if x < 0 {
        println!("Negative");
    } else {
        println!("Zero");
    }

    match number {
        1 => println!("One"),
        2 | 3 => println!("Two or Three"),
        _ => println!("Something else"),
    }
}

Loops

SyntaxDescription
loopInfinite loop (exit with break)
whileLoop while a condition is true
forLoop over an iterator or range
fn main() {
    loop {
        if some_condition {
            break;
        }
    }

    while i < 10 {
        i += 1;
    }

    for x in 0..5 {
        println!("{}", x);
    }
}

Loop control

SyntaxDescription
breakExit the loop
continueSkip the rest of current loop iteration
break 'labelBreak out of a named loop
'label: loop {}Label a loop to break from nested loops
fn main() {
    'outer: for i in 0..3 {
        for j in 0..3 {
            if i == j {
                break 'outer;
            }
        }
    }
}

Comments

Comment TypeSyntaxPurpose
Line Comment//For quick notes or disabling lines
Block Comment/* ... */Multi-line, can be nested
Doc Comment (item)///Public API docs for items
Doc Comment (module)//!Docs for the current file/module

Examples for Doc comments

  1. Documenting a function

    /// Adds two numbers and returns the result.
    ///
    /// # Arguments
    ///
    /// * `a` - The first integer
    /// * `b` - The second integer
    ///
    /// # Example
    ///
    /// ```
    /// let sum = add(2, 3);
    /// assert_eq!(sum, 5);
    /// ```
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }
  2. Documenting a struct

    /// A simple structure to hold a point in 2D space.
    struct Point {
        /// The x-coordinate.
        x: f64,
        /// The y-coordinate.
        y: f64,
    }
  3. Crate level documentation (usually in lib.rs)

    //! # My Math Crate
    //!
    //! This crate provides basic math utilities.
    //!
    //! ## Features
    //! - Addition
    //! - Subtraction
    
    /// Adds two numbers.
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
  4. Module level documentation (usually in mod.rs)

    //! This module handles geometry-related computations.
    
    /// Computes the area of a rectangle.
    pub fn area(width: f64, height: f64) -> f64 {
        width * height
    }

Notes:

  • Markdown works in both /// and //!.
  • Use triple backticks ``` for code blocks.
  • Run cargo doc --open to generate and view documentation in your browser.