For vs for_each
⚓ Rust 📅 2025-08-28 👤 surdeus 👁️ 11The documentation of Iterator::for_each says:
In some cases
for_eachmay also be faster than a loop, because it will use internal iteration on adapters likeChain.
Now I am wondering what kind of cases are there and if there are simple examples of this? I feel like the advice in this documentation is a bit contradicting, i.e. it says for loops are generally more idiomatic but they might be slower. So if performance is important to me does this mean I should always use for_each instead of for loops?
5 posts - 3 participants
🏷️ Rust_feed