5 ms·
Interestingly Index::index is also usually not marked as `#[must_use]` in Rust either.
by the_mitsuhiko 10mo ago
Interestingly Index::index is also usually not marked as `#[must_use]` in Rust either.
- junon 10mo agoI 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.
- the_mitsuhiko 10mo agoBut it's also not marked at the implementation for HashMap's Index impl for instance.
- tialaramex 10mo agoThis didn't seem like a footgun to me, hats["Jim"]; will panic if, in fact "Jim" isn't one of the keys, but what did the hypothetical author expect to happen when they write this? HashMap doesn't implement IndexMut so hats["Jim"] = 26; won't even compile.
- deleted 10mo ago[deleted]
- steveklabnik 10mo agoThis 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.
- OptionOfT 10mo agoUnsure if I misunderstand: Index returns a reference: https://doc.rust-lang.org/std/ops/trait.Index.html#:~:text=self%2C%20index:%20Idx)%20%2D%3E-,&Self::Output 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?
- junon 10mo agoTechnically without any optimizations this would result in a stray LEA op or something but any optimizing compiler (certainly any that support Rust) would optimize it out even at low levels of debug settings.