Skip to main content
TopMiniSite

Back to all posts

How to Create Statically Sized Array In Rust?

Published on
5 min read
How to Create Statically Sized Array In Rust? image

Best Rust Programming Books to Buy in October 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.06 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 The Rust Programming Language

The Rust Programming Language

BUY & SAVE
$16.92 $39.95
Save 58%
The Rust Programming Language
8 Rust Atomics and Locks: Low-Level Concurrency in Practice

Rust Atomics and Locks: Low-Level Concurrency in Practice

BUY & SAVE
$33.13 $55.99
Save 41%
Rust Atomics and Locks: Low-Level Concurrency in Practice
9 Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

BUY & SAVE
$28.90 $49.99
Save 42%
Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes
+
ONE MORE?

In Rust, a statically sized array can be created by specifying the type and size of the array using square brackets. For example, to create a statically sized array of integers with a size of 5, you can write:

let arr: [i32; 5] = [1, 2, 3, 4, 5];

This declares an array named arr that can hold 5 integers of type i32. The values [1, 2, 3, 4, 5] are assigned to the array at declaration. The size of the array cannot be changed once it is defined.

It is important to note that in Rust, arrays are zero-indexed, meaning the first element of the array is accessed using arr[0], the second element using arr[1], and so on. Arrays in Rust are stack-allocated and do not require heap allocation, making them more performant in some cases compared to vectors.

What is the syntax for creating a statically sized array in Rust?

To create a statically sized array in Rust, you can use the following syntax:

let array_name: [type; size] = [initial_value; size];

Here, type specifies the data type of the elements in the array, size specifies the number of elements in the array, and initial_value is an optional initial value for each element in the array.

For example, to create a statically sized array of integers with a size of 5 and all elements initialized to 0, you can write:

let my_array: [i32; 5] = [0; 5];

What is the benefit of using a statically sized array in Rust?

One benefit of using a statically sized array in Rust is that it allows for compile-time guarantees about the size of the array. This means that the compiler can catch mistakes such as out-of-bounds access at compile time, rather than at runtime. This can help prevent bugs and make the code more reliable and easier to maintain. Statically sized arrays also have a known fixed size at compile time, which can make certain operations more efficient and predictable compared to dynamically sized arrays. Additionally, statically sized arrays in Rust are stack-allocated, which can lead to better performance compared to heap-allocated data structures.

One recommended way to initialize a statically sized array with default values in Rust is to use the array_init crate. This crate provides a macro that allows you to easily initialize an array with default values.

Here is an example of how you can use the array_init crate to initialize an array with default values:

use array_init::array_init;

fn main() { let default_value = 0;

// Initialize an array of size 5 with default values
let my\_array = array\_init(|\_| default\_value);

// Print the array
for value in my\_array.iter() {
    println!("{}", value);
}

}

In this example, we first import the array_init crate and create a default value for the array elements. We then use the array_init macro to initialize an array of size 5 with default values. Finally, we iterate over the array and print out each value.

Using the array_init crate is a recommended way to initialize statically sized arrays with default values in Rust because it provides a clean and concise way to do so.

How to concatenate two statically sized arrays in Rust?

In Rust, you can concatenate two statically sized arrays by using the copy_from_slice method. Here is an example of how you can concatenate two arrays of the same type and size:

fn main() { let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6];

let mut result = \[0; 6\]; // Create a new array to hold the concatenated values

result\[0..arr1.len()\].copy\_from\_slice(&arr1); // Copy values from arr1 to result array
result\[arr1.len()..\].copy\_from\_slice(&arr2); // Copy values from arr2 to result array starting from the end of arr1

println!("{:?}", result); // Output: \[1, 2, 3, 4, 5, 6\]

}

In this example, we first create two arrays arr1 and arr2 with three elements each. We then create a new array result with a size of 6 to hold the concatenated values of arr1 and arr2. We use the copy_from_slice method to copy the values from arr1 and arr2 into the result array at the appropriate positions. Finally, we print the concatenated array result.

How to find the index of a specific element in a statically sized array in Rust?

In Rust, you can find the index of a specific element in a statically sized array by using the iter() method followed by the position() method. Here is an example on how to find the index of a specific element in a statically sized array:

fn main() { let arr = [10, 20, 30, 40, 50];

let index = arr.iter().position(|&x| x == 30);

match index {
    Some(i) => println!("Index of 30: {}", i),
    None => println!("Element not found"),
}

}

In this example, we first create a statically sized array arr and then use the iter() method to create an iterator over the array elements. We then use the position() method with a closure that checks if the element is equal to the specific element we are looking for (in this case, 30). The position() method returns an Option that contains the index of the element if found.

Finally, we use a match statement to handle the Option returned by position(). If the element is found, we print out the index, otherwise, we print out a message indicating that the element was not found.