As the book you linked to says:
If you are trying to execute a closure on an iterator for its side effects, use
for
instead.
That way it works, and it’s much clearer to anyone reading the code. You should use map
when you want to transform a vector to a different one.
More Related Contents:
- How do I avoid unwrap when converting a vector of Options or Results to only the successful values?
- How do I write an iterator that returns references to itself?
- Why do I need to import a trait to use the methods it defines for a type?
- Why is adding a lifetime to a trait with the plus operator (Iterator + ‘a) needed?
- Should trait bounds be duplicated in struct and impl?
- Can I do type introspection with trait objects and then downcast it?
- Cannot borrow as mutable more than once at a time in one code – but can in another very similar
- How can I perform parallel asynchronous HTTP GET requests with reqwest?
- Is it considered a bad practice to implement Deref for newtypes?
- Convert a String to int?
- How do I get an owned value out of a `Box`?
- How do I borrow a RefCell, find a key, and return a reference to the result? [duplicate]
- Converting from Option to Option
- Is there a way other than traits to add methods to a type I don’t own?
- What is the difference between `e1` and `&e2` when used as the for-loop variable?
- Why doesn’t println! work in Rust unit tests?
- How do you use parent module imports in Rust?
- What does ‘let x = x’ do in Rust?
- Is it possible to return either a borrowed or owned type in Rust?
- How do I get the minimum or maximum value of an iterator containing floating point numbers?
- How can I call a raw address from Rust?
- Is there a way to perform an index access to an instance of a struct?
- How to take ownership of T from Arc?
- How to use a local unpublished crate?
- Why do I get “type annotations needed” when using Iterator::collect?
- How to generate statically linked executables?
- What is an idiomatic way to collect an iterator of &T into a collection of Ts?
- Why can I iterate over a slice twice, but not a vector?
- How to print structs and arrays?
- Can Rust optimise away the bit-wise copy during move of an object someday?