4 ms·
Generally speaking, I've found Rust terminology confusing. For example, why does the `IntoIter` iterator gives you owned values? What the heck does "Into" mea
by mikekchar 6y ago
Generally speaking, I've found Rust terminology confusing. For example, why does the `IntoIter` iterator gives you owned values? What the heck does "Into" mean to a Rust developer? There are lots of things like that. I've found that fluency in Rust is partly trying to understand the concepts and partly just learning to understand the specific meanings of certain vocabulary. I've likened it to working with Ruby on Rails. There are a lot of useful facilities, but they practically force you to think exactly like the original author -- who doesn't seem to quite speak like I do.
- littlestymaar 6y ago> What the heck does "Into" mean to a Rust developer? Into means it will consume the value (you don't have it anymore): https://doc.rust-lang.org/1.0.0/style/style/naming/conversions.html https://doc.rust-lang.org/1.0.0/style/style/naming/conversio...
- steveklabnik 6y ago(Please note this document is outdated; you’ve linked to the 1.0.0 docs. This particular page is pretty ok but we don’t ship this at all anymore, and haven’t for years.)
- ChrisSD 6y agoI think the maintained equivalent would be in the Rust API guidelines: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv https://rust-lang.github.io/api-guidelines/naming.html#ad-ho...
- ReactiveJelly 6y ago"Into" means it turns the Vec _into_ an iterator. Normally an iterator iterates _over_ a Vec or other collection, without moving or destroying the Vec. There's also iter_mut which iterates over the Vec but allows you to mutate elements in-place. Those are the 3 big groups of iterators: Immutable, mutable, and the ones that consume (i.e. destroy) the vector.
- mikekchar 6y agoI think the thing is that because the IntoIter consumes the collection, you are right that it is turning it "into" an iterator... I guess. But for me that's not the intent of what I'm doing. If I use iter() or mut_iter(), I'm also getting an iterator. The fact that it doesn't consume the collection is not the thing I care about. I don't program thinking "Oh, I want to consume this collection when I create the iterator, so let's use into_iter()". I think, "Oh, I want to get owned objects out of the iterator. As a side effect of that, it will consume the collection". For me, anyway, it's just a seriously backwards way of saying what you want. I find that, similar to my experience with Rails (and Rails developers), once you've drunk the Kool aide, it starts to make sense. You are fluent in the language that is being used and you can't think any other way about it. Even if it is awkward, it seems normal and obvious. But when you first approach it, it can be hard to acquire the idea.
- wtetzner 6y agoI could see that. It was a little confusing to me too. At first I couldn't figure out why anyone would want into_iter(). Didn't realize it returned owned objects. Now that I think about it, how does into_iter() work on a Vec? Is it just copying the data out of the Vec anyway? Because the data in a Vec is allocated on the heap, so how can you get an owned object that isn't just a reference (pointer) to the heap memory without copying? If that's the case, is the only benefit of into_iter() that you don't have to explicitly deref and copy the items?