Expand description
Hint to the compiler that a function is unlikely to be called.
Marking a function #[cold] tells the compiler that calls to it are rare, so it can
optimize for the common case where the function is not called. It is only a hint: the
compiler may ignore it, and it does not change the function’s behavior.
It is typically used on functions that handle uncommon cases, such as error or panic paths:
fn check(value: i32) {
if value < 0 {
report_error("value must be non-negative");
}
// ... the common case continues here ...
}
#[cold]
fn report_error(message: &str) {
eprintln!("error: {message}");
}For more information, see the Reference on the cold attribute.