Skip to content

Commit 6a28d0c

Browse files
committed
Add documentation for the inline attribute
Document the built-in `inline` attribute in the standard library using the `#[doc(attribute = "...")]` mechanism, following the existing `must_use` attribute documentation.
1 parent 267c405 commit 6a28d0c

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

library/std/src/attribute_docs.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,43 @@ mod deprecated_attribute {}
335335
///
336336
/// [the `warn` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
337337
mod warn_attribute {}
338+
339+
#[doc(attribute = "inline")]
340+
//
341+
/// Suggest that the compiler inline a function at its call sites.
342+
///
343+
/// Inlining replaces a call with a copy of the called function's body, which can remove the
344+
/// overhead of the call. The `inline` attribute is only a hint: the compiler may ignore it, and
345+
/// it already inlines functions on its own when that looks worthwhile. Poor choices about what to
346+
/// inline can make a program larger or slower.
347+
///
348+
/// Where it does matter is inlining across crate boundaries. A non-generic function is not
349+
/// normally inlined into another crate, since the calling crate compiles against only its
350+
/// signature. Marking it `#[inline]` makes the body available to other crates so they can inline
351+
/// it too:
352+
///
353+
/// ```rust
354+
/// # #![allow(dead_code)]
355+
/// #[inline]
356+
/// pub fn square(x: i32) -> i32 {
357+
/// x * x
358+
/// }
359+
/// ```
360+
///
361+
/// Generic functions do not need this. They are instantiated in each crate that uses them, so
362+
/// their bodies are already available to inline.
363+
///
364+
/// The attribute applies to functions and has three forms:
365+
///
366+
/// - `#[inline]` suggests inlining the function.
367+
/// - `#[inline(always)]` suggests inlining it at every call site.
368+
/// - `#[inline(never)]` suggests never inlining it.
369+
///
370+
/// You should almost never need `#[inline(always)]`: prefer to let the compiler decide unless
371+
/// profiling shows a small, hot function that benefits from it. `#[inline(never)]` is useful to
372+
/// keep a rarely used path, such as a function that only reports an error, out of its caller.
373+
///
374+
/// For more information, see the Reference on [the `inline` attribute].
375+
///
376+
/// [the `inline` attribute]: ../reference/attributes/codegen.html#the-inline-attribute
377+
mod inline_attribute {}

0 commit comments

Comments
 (0)