this post was submitted on 10 Apr 2024
32 points (100.0% liked)
Rust
5974 readers
80 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I mean, the actual operation is just an example, of course. Feel free to make it a
.map()
operation instead. The strings couldn’t be reused then, but the vector’s allocation still could… in theory.map()
can still be used withVec::iter_mut()
,filter_map()
can be replaced withVec::retain_mut()
.Yeah, that's helpful if I would be currently optimizing a hot loop now. But I was really just using it as an example. Also,
retain_mut()
doesn't compose as well.I'd much rather write:
Over:
And it would be nice if that would be optimized the same. After all, the point of Rust's iterators is to provide zero-cost abstractions. In my opinion, functions like
retain_mut()
represent a leakiness to that abstraction, because the alternative turns out to not be zero cost.To be fair, these alternatives are also limited to the case where the item type stays the same.