logoalt Hacker News

the_mitsuhikoyesterday at 2:21 PM2 repliesview on HN

Interestingly Index::index is also usually not marked as `#[must_use]` in Rust either.


Replies

OptionOfTtoday at 5:17 AM

Unsure if I misunderstand:

Index returns a reference:

https://doc.rust-lang.org/std/ops/trait.Index.html#:~:text=s...

If you don't use the reference it just ... disappears.

Am I missing something here?

junonyesterday at 2:43 PM

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.

show 2 replies