Why .iter() for looping over arrays

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Why .iter() for looping over arrays

Pim Schellart
Dear Rust developers,

I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:

    let a = [1i, 2i, 3i];

    for e in a.iter() {
        println!("{}", e);
    }

why can’t one simply write:

    let a = [1i, 2i, 3i];

    for e in a {
        println!("{}", e);
    }

and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
Please feel free to tell me to RTFM or ask this question elsewhere.

Regards,

Pim
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Manish Goregaokar
For loops only work with objects that implement the Iterator trait.

An array in itself is not an iterator (it doesn't have a "state" as to which element it currently is on), however .iter() gives you an Iter<T>, which is iterable and has a "state".

-Manish Goregaokar

On Thu, Jan 1, 2015 at 4:19 PM, Pim Schellart <[hidden email]> wrote:
Dear Rust developers,

I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:

    let a = [1i, 2i, 3i];

    for e in a.iter() {
        println!("{}", e);
    }

why can’t one simply write:

    let a = [1i, 2i, 3i];

    for e in a {
        println!("{}", e);
    }

and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
Please feel free to tell me to RTFM or ask this question elsewhere.

Regards,

Pim
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Pierre Talbot
In reply to this post by Pim Schellart
Hi,

I guess it is a language design choice due to the low-level nature of
the Rust memory model. It's not like in Java where everything is garbage
collected and there is merely one way to iterate over a collection (by
reference). In Rust you can decide to move elements (with into_iter())
or to get a reference to it (with iter()), choosing a default between
these two may not be desirable. However I'm just supposing since I did
not design the language ^^.

Cheers,
Pierre

On 01/01/2015 11:49 AM, Pim Schellart wrote:

> Dear Rust developers,
>
> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>
>      let a = [1i, 2i, 3i];
>
>      for e in a.iter() {
>          println!("{}", e);
>      }
>
> why can’t one simply write:
>
>      let a = [1i, 2i, 3i];
>
>      for e in a {
>          println!("{}", e);
>      }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.
>
> Regards,
>
> Pim
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Devin Jeanpierre
In reply to this post by Pim Schellart
On Thu, Jan 1, 2015 at 2:49 AM, Pim Schellart <[hidden email]> wrote:

> why can’t one simply write:
>
>     let a = [1i, 2i, 3i];
>
>     for e in a {
>         println!("{}", e);
>     }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.

That's what Python does. But then, in order to be able to iterate over
iterators, iterators must also have iter() methods, which presumably
return self, which is an additional source of implementation errors
and isn't really reasonable because it would have to consume the
original reference, invalidating it, which is super annoying. (You
couldn't do "for x in a {...} a.foobar()"

FWIW there are side benefits to not doing this automatically -- in
Python, the difference between an iterable and an iterator is subtle,
and the selection of "default" iterators for many types is also
confusing. e.g. for dictionaries/mappings, one might reasonably
iterate over keys, values, or key-value pairs, which is the default?

-- Devin
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Masklinn
In reply to this post by Pim Schellart
A big issue is Rust collections can (and often want to) provide multiple
iterators depending whether the yielded values should be references,
mutable references, … Most collections provide both iter() and
iter_mut() for instance, and these are not *views* (in e.g. the Python
sense), they don't iterate on a subset of the collection, instead they
provide a different level of access to the current iteration value.

There have been suggestions about an Iterable trait, there is an open
RFC (https://github.com/rust-lang/rfcs/pull/17) and IIRC the collections
reform (https://github.com/rust-lang/rfcs/pull/235) contains a subset of
it, but so far the explicit iterator has not been a huge wart or issue.
It can also be changed in a completely backwards-compatible manner so it
can be tackled post-1.0.

On 2015-01-01, at 11:49 , Pim Schellart <[hidden email]> wrote:

> Dear Rust developers,
>
> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a.iter() {
>        println!("{}", e);
>    }
>
> why can’t one simply write:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a {
>        println!("{}", e);
>    }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.
>
> Regards,
>
> Pim
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Ryan Hiebert
In reply to this post by Pim Schellart
I asked why there wasn't an Iterable trait in rust, for just this reason, and was informed that the trait would require higher kinded types. I suspect that once those arrive (sometime after 1.0) that for loops may change to use Iterable instead of Iterator.

Sent from my iPhone

> On Jan 1, 2015, at 4:49 AM, Pim Schellart <[hidden email]> wrote:
>
> Dear Rust developers,
>
> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a.iter() {
>        println!("{}", e);
>    }
>
> why can’t one simply write:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a {
>        println!("{}", e);
>    }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.
>
> Regards,
>
> Pim
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Gulshan Singh
I've also heard HKT are need for this trait. See the "Lack of iterator methods" section of this RFC for an explanation: https://github.com/rust-lang/rfcs/blob/master/text/0235-collections-conventions.md

On Thu Jan 01 2015 at 9:54:20 AM Ryan Hiebert <[hidden email]> wrote:
I asked why there wasn't an Iterable trait in rust, for just this reason, and was informed that the trait would require higher kinded types. I suspect that once those arrive (sometime after 1.0) that for loops may change to use Iterable instead of Iterator.

Sent from my iPhone

> On Jan 1, 2015, at 4:49 AM, Pim Schellart <[hidden email]> wrote:
>
> Dear Rust developers,
>
> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a.iter() {
>        println!("{}", e);
>    }
>
> why can’t one simply write:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a {
>        println!("{}", e);
>    }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.
>
> Regards,
>
> Pim
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Ryan Hiebert
In reply to this post by Ryan Hiebert
Thanks for the link. That RFC is a great read.

On Jan 1, 2015, at 11:09 AM, Gulshan Singh <[hidden email]> wrote:

I've also heard HKT are need for this trait. See the "Lack of iterator methods" section of this RFC for an explanation: https://github.com/rust-lang/rfcs/blob/master/text/0235-collections-conventions.md

On Thu Jan 01 2015 at 9:54:20 AM Ryan Hiebert <[hidden email]> wrote:
I asked why there wasn't an Iterable trait in rust, for just this reason, and was informed that the trait would require higher kinded types. I suspect that once those arrive (sometime after 1.0) that for loops may change to use Iterable instead of Iterator.

Sent from my iPhone

> On Jan 1, 2015, at 4:49 AM, Pim Schellart <[hidden email]> wrote:
>
> Dear Rust developers,
>
> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a.iter() {
>        println!("{}", e);
>    }
>
> why can’t one simply write:
>
>    let a = [1i, 2i, 3i];
>
>    for e in a {
>        println!("{}", e);
>    }
>
> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
> Please feel free to tell me to RTFM or ask this question elsewhere.
>
> Regards,
>
> Pim
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Masklinn
In reply to this post by Ryan Hiebert
It'll use both somehow since it can't break existing code. Either by
having iterators be iterables (that's how python does it) or by
iterating iterables desugaring to iterating the corresponding iterator.

On 2015-01-01, at 15:54 , Ryan Hiebert <[hidden email]> wrote:

> I asked why there wasn't an Iterable trait in rust, for just this reason, and was informed that the trait would require higher kinded types. I suspect that once those arrive (sometime after 1.0) that for loops may change to use Iterable instead of Iterator.
>
> Sent from my iPhone
>
>> On Jan 1, 2015, at 4:49 AM, Pim Schellart <[hidden email]> wrote:
>>
>> Dear Rust developers,
>>
>> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>>
>>   let a = [1i, 2i, 3i];
>>
>>   for e in a.iter() {
>>       println!("{}", e);
>>   }
>>
>> why can’t one simply write:
>>
>>   let a = [1i, 2i, 3i];
>>
>>   for e in a {
>>       println!("{}", e);
>>   }
>>
>> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
>> Please feel free to tell me to RTFM or ask this question elsewhere.
>>
>> Regards,
>>
>> Pim
>> _______________________________________________
>> Rust-dev mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why .iter() for looping over arrays

Pim Schellart
Thank you all for the information! I don’t know the language well enough to appreciate all the points in the RFC yet but that will come I guess. The only point mentioned in the emails I disagree with so far is the one about which default to use (as in keys or values for maps) since this choice is already made by whatever “.iter()” is set to. Unless “.iter()” is not guaranteed to exist for all collections in which case there is additional confusion and lookup required on the part of the user.

> On 01 Jan 2015, at 19:36, Masklinn <[hidden email]> wrote:
>
> It'll use both somehow since it can't break existing code. Either by
> having iterators be iterables (that's how python does it) or by
> iterating iterables desugaring to iterating the corresponding iterator.
>
> On 2015-01-01, at 15:54 , Ryan Hiebert <[hidden email]> wrote:
>
>> I asked why there wasn't an Iterable trait in rust, for just this reason, and was informed that the trait would require higher kinded types. I suspect that once those arrive (sometime after 1.0) that for loops may change to use Iterable instead of Iterator.
>>
>> Sent from my iPhone
>>
>>> On Jan 1, 2015, at 4:49 AM, Pim Schellart <[hidden email]> wrote:
>>>
>>> Dear Rust developers,
>>>
>>> I have just started using rust so this is obviously a stupid question but I was wondering why .iter() is needed when looping over the elements of an array? In the following example:
>>>
>>>  let a = [1i, 2i, 3i];
>>>
>>>  for e in a.iter() {
>>>      println!("{}", e);
>>>  }
>>>
>>> why can’t one simply write:
>>>
>>>  let a = [1i, 2i, 3i];
>>>
>>>  for e in a {
>>>      println!("{}", e);
>>>  }
>>>
>>> and have the compiler figure out that ‘a’ has ‘.iter()’ and use it? The form without .iter() just feels more natural to me in this case.
>>> Please feel free to tell me to RTFM or ask this question elsewhere.
>>>
>>> Regards,
>>>
>>> Pim
>>> _______________________________________________
>>> Rust-dev mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/rust-dev
>> _______________________________________________
>> Rust-dev mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/rust-dev
>
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev