Rust Lang

Print a Vector in Rust

Rust is a modern systems programming language that provides the performance of C and C++, while also ensuring memory safety and thread safety.

Vectors are one of the most commonly used data structures in Rust which are dynamic arrays that can grow or shrink in size as needed.

Printing a vector can help for debugging and testing purposes or for displaying the vector’s contents to the user. Rust has multiple ways to print a vector as what we will explore in this post. In addition, we will cover the topics on println! macro or the {::?} format specifier.

Create a Vector in Rust

Let us start at the basics and define a vector in Rust. We can do this using the vec! macro. An example is as follows:

fn main() {
    let rdbms = vec![
        "MySQL",
        "Oracle",
        "Microsoft SQL Server",
        "PostgreSQL",
        "SQLite",
        "IBM DB2",
        "MariaDB",
        "Microsoft Access",
        "SAP HANA",
        "Teradata"
    ];
}

 
In this case, we define a vector that contains the names of the most popular relational database management systems.

Print a Vector Using Macro

The first and most common vector printing method is using the println! macro. This macro allows us to print the formatted text to the console in Rust, followed by a new line. Think of the printf function in C.

The macro takes a format string and zero or more arguments which fill in the placeholders in the format string. The placeholders are indicated by the {} characters in the format string.

We can use this macro to print a vector as demonstrated in the following:

println!("{:?}", rdbms);

 
This should return the contents of the vector on the console:

["MySQL", "Oracle", "Microsoft SQL Server", "PostgreSQL", "SQLite", "IBM DB2",
 "MariaDB", "Microsoft Access", "SAP HANA", "Teradata"]

 

One advantage of using the println! macros is that the data type of the vector does not matter. This is because it is implemented in the vector data type which allows any vector value to use.

Print a Vector through Iteration

As you can guess, we can use a native Rust for loop to iterate and print over the elements of a vector using the println! macro as demonstrated in the following example:

fn main() {
    let rdbms = vec![
        "MySQL",
        "Oracle",
        "Microsoft SQL Server",
        "PostgreSQL",
        "SQLite",
        "IBM DB2",
        "MariaDB",
        "Microsoft Access",
        "SAP HANA",
        "Teradata"
    ];
    for element in rdbms.iter() {
        println!("{}", element);
    }
}

 
Unlike using the println! macro and passing the vector as the argument, iterating over each element in the vector using the for loop allows you to capture the value and do something with it.

For printing purposes, the previous code should return the output as follows:

MySQL
Oracle
Microsoft SQL Server
PostgreSQL

 

Conclusion

We discussed how we can use the println! macro and the Rust for loop to print the contents of a Rust vector in easy steps.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list