Rust Reference ยท Attributes
The #[inline] attribute
Reference for Rust #[inline]: the three forms, what the compiler does with each hint, cross-crate inlining, when to use them and when to leave them out.
Inlining replaces a function call with a copy of the function body. It removes call overhead and lets the optimizer see across the boundary, but it makes code bigger. #[inline] is a hint about this trade-off; the compiler is free to ignore the weaker forms.
There are three forms. #[inline] suggests inlining, and also makes the function's body available to other crates so they can inline it. #[inline(always)] asks strongly. #[inline(never)] asks the compiler not to inline the function at all.
#[inline] fn square(x: u32) -> u32 { x * x }
Try it
Inlining changes performance and code size, not results, so the examples show where the hints go and that the behaviour is the same either way.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
#[inline] #[inline(always)] #[inline(never)]
| Form | Meaning |
|---|---|
#[inline] |
Suggest inlining; make the body visible across crates |
#[inline(always)] |
Strong request to inline at every call site; still not a guarantee in every case |
#[inline(never)] |
Do not inline |
The attribute applies to functions and methods, including trait method implementations and closures.
Edge cases:
- Recursive functions cannot be fully inlined into themselves.
#[inline(always)]on a function that is used as a function pointer still creates an out-of-line copy for the pointer.- Misuse is not an error: the compiler accepts the attribute everywhere it is allowed.
Usage notes
- Default behaviour. Within one crate, LLVM already decides well, and with optimizations on, it inlines small functions without hints. A function without
#[inline]in a library cannot be inlined into other crates, unless it is generic or the build uses link-time optimization. - When
#[inline]helps. Small, hot, non-generic public functions in a library (getters, thin wrappers, arithmetic helpers). - When
alwayshurts. It increases code size and compile time, can worsen instruction-cache use, and can make debugging harder. Measure before using it. #[inline(never)]is useful for keeping rarely used error paths out of hot loops, and for benchmarks and profiling where you need a distinct frame.- Debug builds mostly ignore these hints, so timing results in debug mode are not meaningful for them.
- Related hints:
#[cold]marks a function as unlikely to run, which also discourages inlining it.