Rust has compound data types that allow you to group multiple values into a single type. The two primitive compound types available in Rust are tuples and arrays.
What is a tuple in Rust?
A tuple is a general way to group together a fixed number of values, which can have a variety of different types, into one compound type. Tuples have a fixed length; once declared, they cannot grow or shrink in size.
create a tuple by writing a comma-separated list of values inside parentheses. Each position in the tuple has a type, and as mentioned, the types of the different values within the tuple do not have to be the same. The declaration of tuples looks like arrays, but uses round parentheses instead of square brackets.
Access the elements of a tuple by their position (or index), starting at 0. This is done using the dot notation (.) followed by the index. For example, tuple.0 accesses the first element. Tuple element expressions always refer to a fixed element, like t.4; you cannot use a variable index like t.i or t[i].
Example:
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {}", y);
}
Output:
The value of y is: 6.4
Example:
fn main() {
let x: (i32, f64, u8) = (50, 60.4, 10);
let a = x.0;
let b = x.1;
let c= x.2;
println!("The value is: {},{},{}",a,b,c);
}
Output:
The value is: 50,60.4,10
Accessing fields using dot notation:
Example:
fn main() {
let t: (i8, bool) = (7, true);
dbg!(t.0);
dbg!(t.1);
}
Output:
[src/main.rs:3:5] t.0 = 7
[src/main.rs:4:5] t.1 = true
What are arrays in Rust?
An array is another way to collect multiple values. Different from a tuple, every element of an array has the same type. Arrays in Rust have a fixed length; once declared, they cannot grow or shrink in size. The array’s size is a constant determined at compile time and is part of its type. For example, [u8; 3] and [u8; 4] are considered different types.
In Rust, array values are written as a comma-separated list inside square bracket []. Example using a list of values:
let a = [1, 2, 3, 4, 5];
Alternatively, arrays can be defined with a repeat expression using [value; repeat], which includes a value and a repeat count (the size of the array). Each element will be set to the specified value. This is useful for initializing all elements to the same value, especially for large arrays.
Example using a repeat expression:
let a = [3; 5];it means let a = [3,3,3,3,3];
Square brackets are used to indicate the type of an array, followed by the type of each element, a semicolon, and finally the number of items included within the array.
Example:
fn main() {
let a: [i32; 5] = [1, 2, 3, 4, 5];
let b: [f64; 5] = [1.7, 2.7, 3.9, 4.3, 5.4];
println!("The value is: {},{},{},{}",a[0],b[4],a[3],b[3]);
}
Output:
The value is: 1,5.4,4,4.3
In the above example, i32 & f64 denote the type of each element. The numeral 5 following the semicolon signifies that the array comprises five members.
An Access individual elements of an array using square brackets and the element’s index. Array indexing uses array_name[index]. Access to an array is always bounds-checked in safe methods and operators. Example accessing an element:
Example:
fn main() {
let x: [f64;3] = [50.1, 60.5, 10.5];
let a = x[1];
let b = x[0];
println!("The value is: {},{}",a,b);
}
Output:
The value is: 60.5,50.1
Most arrays are single dimensional, but multidimensional arrays are also possible, consisting of rows, columns, depth, and so on. There is no intrinsic limit to dimensions except comprehension and readability. The syntax for declaring a multidimensional array involves nested square brackets. The len function applied to a multidimensional array by default gives the length of the first dimension.
Finally, tuples group a fixed number of values of different types, accessed by numeric index, while arrays group a fixed number of values of the same type, accessed by index. Both are fixed-size structures, distinct from dynamic collections like vectors.
What is the difference between tuples and arrays in Rust?
Tuples and arrays are both compound types in Rust, used to group multiple values together.
A key distinction lies in the types of the values they can hold. Arrays store a fixed number of elements, but all elements must be of the same type. An array’s size is a compile-time constant and is part of its type. Tuples can group a fixed number of values, but these values can have a variety of different types.
Both tuples and arrays have a fixed length once declared; they cannot grow or shrink. Accessing elements in a tuple uses dot notation with a constant index, such as tuple.0. Array elements are accessed using square brackets with a usize index, like array[i]. Unlike tuples, arrays can be easily iterated over.
Example:
fn main() {
// Tuple: groups different types, fixed length, access by index or destructure
let my_tuple = ("hello", 10, true); // tuple containing a string slice, integer, and boolean
let message = my_tuple.0; // Accessing the first element by index
println!("Tuple first element: {}", message); // Output: hello
let (text, number, status) = my_tuple; // Destructuring the tuple
println!("Destructured: {} {} {}", text, number, status); // Output: hello 10 true
}
fn main() {
// Array: groups same type, fixed length (size is part of type), access by index
let my_array: [i32; 3] =[89,58,26]; // array of three i32 integers
let first_element = my_array[2]; // Accessing the first element by index
println!("Array first element: {}", first_element); // Output: 1
// Arrays can be iterated over easily
for value in my_array {
println!("Array value: {}", value); // Output: Array value: 1, Array value: 2, etc.
}
}
Output:
Tuple first element: hello
Destructured: hello 10 true
Array first element: 26
Array value: 89
Array value: 58
Array value: 26