DEV Community

Cover image for Day 4: Exploring Rust's Compound Data Types
Aniket Botre
Aniket Botre

Posted on • Edited on

Day 4: Exploring Rust's Compound Data Types

Greetings, fellow coders! ๐Ÿš€ On Day 4 of my #100DaysOfCode adventure with Rust, I delved into the fascinating world of compound data types โ€“ Tuples and Arrays. Let's unravel the key properties and differences between these structures.

Tuples: A Symphony of Heterogeneity

Tuples in Rust serve as harmonious ensembles, allowing the storage of heterogenous data types. Here are some key properties:

  • Fixed Length: Tuples maintain a fixed length, ensuring structural integrity.

  • Diverse Data Types: They embrace diversity, accommodating elements of different data types.

let tup: (i32, f64, u8) = (500, 6.4, 1);
Enter fullscreen mode Exit fullscreen mode
  • Accessing Elements: Elements are accessed using the dot operator . along with the index.
let tup: (i32, i32, i32) = (1, 2, 3);
let first = tup.0; // 1
let second = tup.1; // 2
let third = tup.2; // 3
Enter fullscreen mode Exit fullscreen mode
  • Destructuring Magic: Tuples can be deconstructed to unveil individual elements.
let tup = (1, 2, 3);
let (x, y, z) = tup;
println!("The value of x is {x}, the value of y is {y}, the value of z is {z}");
// The value of x is 1, the value of y is 2, the value of z is 3
Enter fullscreen mode Exit fullscreen mode

Note: An empty tuple without any values has a special name โ€“ unit (), representing empty values or an empty return type.


Arrays: Stacking Homogeneity

Arrays in Rust provide a stack-based haven for homogenous data types. Here's a glimpse of their characteristics:

  • Fixed Length: Arrays uphold a fixed length, ideal for scenarios with a predetermined number of elements.

  • Homogeneous Storage: They store similar data types, ensuring consistency.

let a = [1, 2, 3, 4, 5];
let first = a[0]; // 1
let second = a[1]; // 2
Enter fullscreen mode Exit fullscreen mode
  • Destructuring Arrays: Similar to tuples, arrays can be destructured to extract individual elements.
let a = [1, 2];
let [first, second] = a;
println!("The value of first is {first}, the value of second is {second}");
// The value of first is 1, the value of second is 2
Enter fullscreen mode Exit fullscreen mode

Differences and Similarities

Differences:

  1. Arrays are homogenous; tuples are heterogenous.

  2. Tuples are stored in heap memory; arrays are stored in stack memory.

  3. Elements of an array are stored in contiguous memory locations.

  4. Tuples are initialized using parentheses (), while arrays use square brackets [].

Similarities:

  1. Both have a fixed length.

  2. Both support destructuring.

  3. Both are used to store multiple values.

  4. Both are stored in stack memory.

  5. Individual elements can be accessed using indices.

As I navigate the terrain of compound data types in Rust, the language's versatility and precision continue to captivate me. Follow my coding journey on Github for more updates! ๐Ÿ’ป๐ŸŒโœจ

RustLang #Programming #LearningToCode #CodeNewbie #TechJourney #Day4

Top comments (0)