TL;DR: I'm proposing ability to set
#[must_use]
flag once for all functions in an entire crate/module/impl block.
"must use" behavior in Swift is close to what Rust does, except the default for functions is "must use". Instead of having an opt-in #[must_use]
, Swift has a @discardableResult
annotation that opts out of the default "must use" behavior. I find Swift's default quite sensible and useful.
When I review my code for functions which could have #[must_use]
, I find that pretty much all of them could. There's a big gray area, but I find that "definitely discardable" results are a minority.
The different default also changes thinking about this feature. Instead of hypothetical "would anyone actually forget to use this value, and would that be a disaster?" to "is ignoring return value of this function part of its intended use?", which is more grounded in API design.
Adding #[must_use]
everywhere is a bit of a chore and peppers code with annotations.
I realize Rust can't actually change the default without causing an annoying transition period, therefore I suggest adding an opt-in syntax that flips the #[must_use]
default for entire scopes instead.
For example, a crate could have:
#![every_fn_is_must_use]
or module, or impl:
#[every_fn_is_must_use]
impl VeryMuchUse {
…
}