archives of rust-dev@mozilla.org (2010-2015)
 help / color / mirror / Atom feed
* [rust-dev] Why .iter() for looping over arrays
@ 2015-01-01 10:49 Pim Schellart
  2015-01-01 11:10 ` Manish Goregaokar
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Pim Schellart @ 2015-01-01 10:49 UTC (permalink / raw)
  To: rust-dev

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
@ 2015-01-01 11:10 ` Manish Goregaokar
  2015-01-01 11:15 ` Devin Jeanpierre
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Manish Goregaokar @ 2015-01-01 11:10 UTC (permalink / raw)
  To: Pim Schellart; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 1320 bytes --]

For loops only work with objects that implement the Iterator
<http://doc.rust-lang.org/std/iter/trait.Iterator.html> 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>
<http://doc.rust-lang.org/std/slice/struct.Iter.html>, which is iterable
and has a "state".

-Manish Goregaokar

On Thu, Jan 1, 2015 at 4:19 PM, Pim Schellart <p.schellart@gmail.com> 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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>

[-- Attachment #2: Type: text/html, Size: 1974 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
  2015-01-01 11:10 ` Manish Goregaokar
@ 2015-01-01 11:15 ` Devin Jeanpierre
  2015-01-01 11:16 ` Pierre Talbot
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Devin Jeanpierre @ 2015-01-01 11:15 UTC (permalink / raw)
  To: Pim Schellart; +Cc: rust-dev

On Thu, Jan 1, 2015 at 2:49 AM, Pim Schellart <p.schellart@gmail.com> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
  2015-01-01 11:10 ` Manish Goregaokar
  2015-01-01 11:15 ` Devin Jeanpierre
@ 2015-01-01 11:16 ` Pierre Talbot
  2015-01-01 12:08 ` Masklinn
  2015-01-01 14:54 ` Ryan Hiebert
  4 siblings, 0 replies; 10+ messages in thread
From: Pierre Talbot @ 2015-01-01 11:16 UTC (permalink / raw)
  To: rust-dev

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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
                   ` (2 preceding siblings ...)
  2015-01-01 11:16 ` Pierre Talbot
@ 2015-01-01 12:08 ` Masklinn
  2015-01-01 14:54 ` Ryan Hiebert
  4 siblings, 0 replies; 10+ messages in thread
From: Masklinn @ 2015-01-01 12:08 UTC (permalink / raw)
  To: rust-dev

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 <p.schellart@gmail.com> 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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
                   ` (3 preceding siblings ...)
  2015-01-01 12:08 ` Masklinn
@ 2015-01-01 14:54 ` Ryan Hiebert
  2015-01-01 17:10   ` Gulshan Singh
                     ` (2 more replies)
  4 siblings, 3 replies; 10+ messages in thread
From: Ryan Hiebert @ 2015-01-01 14:54 UTC (permalink / raw)
  To: Pim Schellart; +Cc: rust-dev

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 <p.schellart@gmail.com> 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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 14:54 ` Ryan Hiebert
@ 2015-01-01 17:10   ` Gulshan Singh
       [not found]   ` <CANEZYrcqGYz5Hg-JYoL6z=6uKo_eeLThBaQPKG1NJkCP4YyY7Q@mail.gmail.com>
  2015-01-01 18:36   ` Masklinn
  2 siblings, 0 replies; 10+ messages in thread
From: Gulshan Singh @ 2015-01-01 17:10 UTC (permalink / raw)
  To: Ryan Hiebert, Pim Schellart; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 1712 bytes --]

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 <ryan@ryanhiebert.com> 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 <p.schellart@gmail.com> 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
> > Rust-dev@mozilla.org
> > https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>

[-- Attachment #2: Type: text/html, Size: 2675 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
       [not found]   ` <CANEZYrcqGYz5Hg-JYoL6z=6uKo_eeLThBaQPKG1NJkCP4YyY7Q@mail.gmail.com>
@ 2015-01-01 18:18     ` Ryan Hiebert
  0 siblings, 0 replies; 10+ messages in thread
From: Ryan Hiebert @ 2015-01-01 18:18 UTC (permalink / raw)
  To: Gulshan Singh; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 2123 bytes --]

Thanks for the link. That RFC is a great read.

> On Jan 1, 2015, at 11:09 AM, Gulshan Singh <gsingh2011@gmail.com> 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 <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 <ryan@ryanhiebert.com <mailto:ryan@ryanhiebert.com>> 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 <p.schellart@gmail.com <mailto:p.schellart@gmail.com>> 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
> > Rust-dev@mozilla.org <mailto:Rust-dev@mozilla.org>
> > https://mail.mozilla.org/listinfo/rust-dev <https://mail.mozilla.org/listinfo/rust-dev>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org <mailto:Rust-dev@mozilla.org>
> https://mail.mozilla.org/listinfo/rust-dev <https://mail.mozilla.org/listinfo/rust-dev>


[-- Attachment #2: Type: text/html, Size: 3648 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 14:54 ` Ryan Hiebert
  2015-01-01 17:10   ` Gulshan Singh
       [not found]   ` <CANEZYrcqGYz5Hg-JYoL6z=6uKo_eeLThBaQPKG1NJkCP4YyY7Q@mail.gmail.com>
@ 2015-01-01 18:36   ` Masklinn
  2015-01-02 13:47     ` Pim Schellart
  2 siblings, 1 reply; 10+ messages in thread
From: Masklinn @ 2015-01-01 18:36 UTC (permalink / raw)
  To: rust-dev

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 <ryan@ryanhiebert.com> 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 <p.schellart@gmail.com> 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
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [rust-dev] Why .iter() for looping over arrays
  2015-01-01 18:36   ` Masklinn
@ 2015-01-02 13:47     ` Pim Schellart
  0 siblings, 0 replies; 10+ messages in thread
From: Pim Schellart @ 2015-01-02 13:47 UTC (permalink / raw)
  To: rust-dev

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 <masklinn@masklinn.net> 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 <ryan@ryanhiebert.com> 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 <p.schellart@gmail.com> 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
>>> Rust-dev@mozilla.org
>>> https://mail.mozilla.org/listinfo/rust-dev
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
> 
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-01-02 13:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-01 10:49 [rust-dev] Why .iter() for looping over arrays Pim Schellart
2015-01-01 11:10 ` Manish Goregaokar
2015-01-01 11:15 ` Devin Jeanpierre
2015-01-01 11:16 ` Pierre Talbot
2015-01-01 12:08 ` Masklinn
2015-01-01 14:54 ` Ryan Hiebert
2015-01-01 17:10   ` Gulshan Singh
     [not found]   ` <CANEZYrcqGYz5Hg-JYoL6z=6uKo_eeLThBaQPKG1NJkCP4YyY7Q@mail.gmail.com>
2015-01-01 18:18     ` Ryan Hiebert
2015-01-01 18:36   ` Masklinn
2015-01-02 13:47     ` Pim Schellart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox