Use the #[must_use] attribute on functions to trigger compiler warnings when their return values are ignored.
Add the #[must_use] attribute above a function definition to force the compiler to warn if the return value is ignored.
#[must_use]
fn gives_ownership() -> String {
let some_string = String::from("yours");
some_string
}
fn main() {
gives_ownership(); // Warning: unused return value
}
The #[must_use] attribute tells the compiler to alert you if you forget to use the result a function gives you. It acts like a safety net to prevent bugs where critical data, like a new file handle or a calculated value, is accidentally discarded. Think of it as a reminder to catch the ball when someone throws it to you.