Rust, Software Development

Rust Array Size Limit To 32 No More In 1.47.0 Release

Rust lang

Before 1.47.0, there is the Rust array size limit size of 32. This is evident when we get the E0277 error that has the following message.

When Do We Get The Error?

Personally, I have never encountered this error until I read the 1.47.0 release notes.

Rust does not currently have a way to be generic over integer values. This has long caused problems with arrays, because arrays have an integer as part of their type; [T; N] is the type of an array of type T of N length. Because there is no way to be generic over N, you have to manually implement traits for arrays for every N you want to support. For the standard library, it was decided to support up to N of 32.

Apparently, this error happens when we treat arrays as generic values. Consider the following examples using Rust 1.46.0.

Example 1, Using format!

When we compile these codes, we will get an error on line 9.

Example 2, Using print!

Now, consider the following codes that use the print! macro on line 9.

We will get the following error.

Example 3, Trying To Make Things Generic

This is for demonstration only! Another way to look at the Rust array size limit size issue is through the following codes. We can definitely do the following.

However, the following codes will not compile where we try to make the array size generic using the i32 type instead of a constant value.

We will get the following error.

Trying Out With Rust 1.47.0

Before moving on, make sure you have updated your Rust to 1.47.0.

Then, you may need to restart your IDE. This time, when we run the following codes, everything will work.

The output generated is as follows.

Note that Rust 1.47.0 does not address all issues related to the Rust Array Size Limit of 32.

We have been working on a feature called “const generics” that would allow you to be generic over N. Fully explaining this feature is out of the scope of this post, because we are not stabilizing const generics just yet. However, the core of this feature has been implemented in the compiler, and it has been decided that the feature is far enough along that we are okay with the standard library using it to implement traits on arrays of any length.

This post is part of the Rust Programming Language For Beginners Tutorial.

You Might Also Like