How to Use #[inline] and When Does It Help?

The #[inline] attribute suggests to the compiler to replace function calls with the function body to reduce overhead in performance-critical code.

The #[inline] attribute hints to the compiler to replace a function call with the function's body to reduce call overhead, but it is only a suggestion and the compiler may ignore it. It helps most in small, frequently called functions within tight loops where the cost of the call outweighs the cost of duplicating the code.

#[inline]
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let result = add(2, 3);
}