Wrapping my head around Rust (Part 1)
Rust is a very interesting language. I’m trying to learn it to speed up critical parts in Ruby, also to process massive amounts of data.
However, it has odd concepts to anyone coming from other languages. I’m trying to get how it works. One of the worst things so far is understanding what the “cannot move out of borrowed content” error means.
So far, I learned:
- Iterators are “consumable”! You can’t lend it to a function and iterate over it. You need to transfer ownership to the function (i.e. don’t pass an iterator as argument using “&”).
- Once you use an Iterator with “map”, “filter”, etc… it’s done. You can’t reuse it. That’s because most methods in Iterator receive the ownership of the struct (i.e. “self”, not “&self”).
Corolary: iterators are disposable. Create them just before use them, and let them behind.