Logo

Programming-Idioms

History of Idiom 7 > diff from v108 to v109

Edit summary for version 109 by programming-idioms.org:
Restored version 107: Other way, other impl entry please

Version 108

2021-06-09, 08:53:43

Version 109

2021-06-20, 15:31:46

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Code
items.iter().enumerate().for_each(|(i, x)| {
    println!("Item {} = {}", i, x);
})
Code
for (i, x) in items.iter().enumerate() {
    println!("Item {} = {}", i, x);
}
Doc URL
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate
Doc URL
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate
Demo URL
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b06e747fd4f280b7692fb02dd041f31e
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=88969b976961b1302d199e00ca741db9