Rust:- Iterating a list iter().enumerate()

Background

In time, you will need to build out a list.

 

Basic Steps

The basic steps are the same:-

  1. Declare a list
  2. Initialize the list
  3. Add items to the list
  4. Display list items

 

Script

Outline

  1. Create a list
    • A list of fruits
  2. Declare variables
    • i_plus_1:usize
  3. for loop
      • Prepare Loop
        • for (i, x)
        • in
        • items.iter().enumerate()
      • Set Contextual variables
        • i_plus_1
          • i_plus_1 = i + 1
      • Print List Item
    println!(
                          "Item {0} = {1}"
                           
                        , i_plus_1
                         
                        , x
                         
            );
    

Code


fn main() 
{
    
    let items = [
                      "orange"
                    , "apple"
                    , "pear"
                    , "tangerine"
                    , "watermelon"
                ];
    
    let mut i_plus_1:usize;
    
    for (i, x) in items.iter().enumerate() 
    {
    
        i_plus_1 = i + 1;
        
        println!(
                      "Item {0} = {1}"
                      
                    , i_plus_1
                    
                    , x
                    
                );
        
    }
    
}

Output

Image

Textual

Item 1 = orange
Item 2 = apple
Item 3 = pear
Item 4 = tangerine
Item 5 = watermelon

Online Source Code

OnlineGDB

  1. rustListAppleIterEnumerate.rs
    Link

Source Code Control

GitLab

  1. rustListAppleIterEnumerate.rs
    Link

    • Files
      • list.apple.iter.enumerate.rs

 

Summary

There are a couple of things that are relatively unique about Rust.

Such as:-

  1. Data Types
    • usize
      • An actual datatypes for sizes

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s