Rust Array and Tuple

Array

Historically, the word array means to put in order. Up until now, even in our programming world, not much has changed about this definition. The Array data structure ensures elements entered into it are orderly.

The array is a collection of items of the same type, T, stored in an orderly sequence - contiguous - in memory.

The following are the characteristics of the array in Rust:

  • Immutability

    In some languages, items in an array can be deleted or a new item inserted.

      let arr = [];
      arr.push("brother");
      arr.push("bernard");
      arr.pop();
    
      // print arr
      console.log(arr); // ["brother"]
    

    In Rust, this manipulation (inserting and deleting) can't be done to an Array. Hence, where it got its first characteristic, immutability.

    Explicit item

    Array type must be explicitly written. An empty array is useless and unacceptable.

Write your first array

Writing an array can take two forms:

  • List, [x, y, z] where x,y,z are the same type: in this approach, each array item - x, y, z - is listed in the bracket, [], like this

    • [1, 2, 34],

    • ["Jan", "Feb", "March", "April"],

    • [[2, 6], [12, 5], [4, 7], [11, 55]].

  • Repeat expression, [x; N] where N is a non-negative integer & x is the array item: This produces an array with N copy of x. See example below

    • [1; 3],

    • [["Jan", "Dec"]; 12]

Retrieving items in an array

You can retrieve an item with

  • An array index. This is the index of an item, arr[index]. The index must be a non-negative integer.

  • Iterator, .iter(). You can use for..in and .iter() to retrieve items.

Tuple

A tuple is a finite ordered list of elements. It is similar to an array with some additional features and limitations alike. The ability to hold multiple types is one of the additional features in a tuple compared to an array - in fact, the outstanding feature.

One of Tuple's limitations is that it is not iterable. Unlike Array where we can use an iterator to access its item, Tuples lack this ability.

Historically, the word tuple originated from the abstraction of sequence: Single, Couple/Double, Triple, Quadruple, Quintuple etc.

TupleLengthExample in Rust
0empty tuple( )
1monuple( "Kelvinsekx" )
2couple("Kelvinsekx", 25)
3triple("Kelvinsekx", 25, "Ibadan")
4quadruple("Kelvinsekx", 25, "Ibadan", Sex::Male)
5quintuple("Kelvinsekx", 25, "Ibadan", Sex::Male, ["Excellent", "Dave"])

Characteristics of Tuples

  • Like in arrays, there is no inbuilt method of inserting or deleting an item in a tuple.

  • You can not iterate over a tuple using a loop.

  • Unlike Array which doesn't support empty array, a tuple supports an empty or null tuple.

  • Tuple is written by listing the element in parentheses.

Write your first Tuple

A tuple can be written by listing items in a parenthesis, ( item1, item2...n ). The table above is a perfect example.

Retrieving items from a Tuple

An item in a tuple can be accessed in two ways

  • tuple index, tuple.INDEX where INDEX is a non-negative number. For example

      let tt = ("Thanks", "for", "reading");
    
      tt.0 // "Thanks"
      tt.1 // "for",
      tt.3 // "reading
    
  • tuple destructuring is a naming pattern of retrieving an item from a tuple. An example is the best way to explain this.

      let (str1, str2, str3) = ("Thanks", "for", "reading");
      str1 // "Thanks",
      str2 // "for",
      str3 // "reading"
    

    See a little detailed use of the image

Thank you for reading thus far. If you find this useful, do not hesitate to drop me a line. It is one of the painless ways to reward my hard work.