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

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to