I see three things going on here. Let's break it down.
The Idea of an Iterator
When you call C++'s std::begin
and Rust's .iter()
in your examples, you are receiving two "types of objects" that are conceptually identical: An iterator.
If we forget about the implementation details for a moment, we can see the purpose and usability of an iterator happen to be similar in both languages. We find that both iterators:
- Are "objects" which can be created from a collection (an "Iterable type")
- Can be advanced using C++'s
std::advance
and Rust's .next()
- Have an "end," determined by C++'s
std::end
and the output of Rust's .next()
.
This is a gross oversimplification of course, they are similar and different in many other ways, but this is probably the general overview you're looking for.
The Implementation of an Iterator
Despite sharing common themes, C++ and Rust are very different languages and will naturally implement a single idea differently. Iterators are no exception.
The "why" is too broad to really answer here on Stack Overflow. It's like asking why oranges are orange and bananas are not :)
But you do seem somewhat confused about how to work with Rust's implementation of iterators, given your comment:
I couldn't find any exact definition of a pointer in the Rust docs
Don't think like a C++ programmer right now. Check out The Book if you haven't already and explore the concepts of borrowing and ownership; It is the far more typical way you will work with data, and it is required to understand how Rust's iterators work.
The Syntactical Sugar for Iterators
Both C++ and Rust have "magic" in their for
loops that let you work with iterator "types" easily.
Contrary to your question, this is not a concept unique to Rust. In C++ an object can be used with the modern for (item : collection)
syntax if it implements special methods, similar to the Iterator
trait you pointed out.
Summary
What are the main differences?
Not much conceptually.
Why does Rust have iterators in this fashion and why are they expressed so differently?
It be like it is because it do. They are more similar than you believe.
Are there Rust-type iterators in C++? Are there C++-type iterators in Rust?
They are conceptually identical.
Are they called something specific? (Internal/External?)
There might be some fancy academic terminology for the implementation differences but I'm not aware of it. An iterator is an iterator.