logoalt Hacker News

junonyesterday at 2:43 PM2 repliesview on HN

I don't believe you can mark trait methods with #[must_use] - it has to be on the implementation. Not near a compiler to check at the moment.

In the case of e.g. Vec, it returns a reference, which by itself is side-effect free, so the compiler will always optimize it. I do agree that it should still be marked as such though. I'd be curious the reasons why it's not.


Replies

steveklabnikyesterday at 3:59 PM

This is just my take, but I think historically the Rust team was hesitant to over-mark things #[must_use] because they didn't want to introduce warning fatigue.

I think there's a reasonable position to take that it was/is too conservative, and also one that it's fine.

the_mitsuhikoyesterday at 3:51 PM

But it's also not marked at the implementation for HashMap's Index impl for instance.

show 1 reply