If I return a direct type that is must_use, I get a helpful compiler warning:
use std::iter::{self, Empty, Skip};
fn x() -> Skip<Empty<i32>> {
iter::empty().skip(1)
}
fn main() {
x();
}
warning: unused `std::iter::Skip` which must be used
--> src/main.rs:8:5
|
8 | x();
| ^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: iterator adaptors are lazy and do nothing unless consumed
The equivalent (and I believe preferred) code using impl Trait gives no such useful feedback:
use std::iter;
fn x() -> impl Iterator<Item = i32> {
iter::empty().skip(1)
}
fn main() {
x();
}
Amusingly, this isn't a new problem, as returning a boxed trait object also has the same problem. It's arguably worse there because you've performed an allocation for no good reason, but it's also less likely because people are more reluctant to introduce unneeded allocations.
If I return a direct type that is
must_use, I get a helpful compiler warning:The equivalent (and I believe preferred) code using
impl Traitgives no such useful feedback:Amusingly, this isn't a new problem, as returning a boxed trait object also has the same problem. It's arguably worse there because you've performed an allocation for no good reason, but it's also less likely because people are more reluctant to introduce unneeded allocations.