Print Arrays in Rust

Rust is a systems programming language that runs fast, controls segfaults, and ensures thread safety. It’s excellent for writing low-level code, such as device drivers and operating systems.
Rust also provides higher-level features that make it easier to build applications.
To print arrays in Rust, it’s not as simple as just printing out the array you want to see either. The Rust compiler will need to know what struct or array you want to print out to generate the appropriate code.
This tutorial will look at how to print arrays in Rust programming.
Create Arrays in Rust
A group of things of a similar sort stored in a connected memory area is referred to as an array. Arrays are helpful because they allow us to store many values together in one variable and access them by index.
Arrays in Rust are created using square brackets: []
. The first value is the type of element that will be stored inside the array, and after that comes the name of each array element, separated by commas.
For example:
let my_array = [1, 2]; // An integer array containing two elements
let my_string_array = ["hello", "world"]; // A string array containing two elements
Mainly, Rust supports two primary ways for constructing arrays:
- An array containing a list of items is used during initialization.
- Repeat expression that repeats a specific element several times.
The repeat array syntax is as follows: [N, X]
. This generates an array consisting of the items x repeated n times.
Declare and Initialize Arrays in Rust
Declaring an array is as easy as declaring any other variable:
let my_array = [1, 2, 3];
let my_array: Vec = vec![1, 2, 3];
The first way is more verbose but has the advantage that you can add more elements to it later on without having to recreate it.
Moreover, the following code declares a new array of strings with a different type (i.e., char) and sets its length to 5:
let letters = ['a', 'b', 'c', 'd', 'e'];
Steps to Print an Array in Rust
A Rust program is composed of many smaller functions. These functions are called closures and can be passed around like any other variable.
The println!
function prints the string passed to it, followed by a newline (a line break). When printing an array in Rust, we employ the ?
operator inside the println!
function.
The ?
operator will print the given expression if it evaluates to true; otherwise, it will return nothing.
In short, you should follow the following steps.
-
Create a variable for the array.
-
Create a variable for the size of the array.
-
Initialize the array with values.
-
Print out each value in the array using the
?
operator inside theprintln!
function.
Let’s discuss an example.
fn main() {
let demo:[&str; 7] = ["Adil"; 7];
println!("Array {:?}", demo);
}
Output:
Array ["Adil", "Adil", "Adil", "Adil", "Adil", "Adil", "Adil"]
Click here to check the live demonstration of the code mentioned above.
Muhammad Adil is a seasoned programmer and writer who has experience in various fields. He has been programming for over 5 years and have always loved the thrill of solving complex problems. He has skilled in PHP, Python, C++, Java, JavaScript, Ruby on Rails, AngularJS, ReactJS, HTML5 and CSS3. He enjoys putting his experience and knowledge into words.
Facebook