Array length is constrained by the isize
type, which is a platform-sized integer:
The isize
type is a signed integer type with the same number of bits as the platform's pointer type. The theoretical upper bound on object and array size is the maximum isize
value. This ensures that isize
can be used to calculate differences between pointers into an object or array and can address every byte within an object along with one byte past the end.
- 16-bit platforms: A maximum value of 215 - 1
- 32-bit platforms: A maximum value of 231 - 1
- 64-bit platforms: A maximum value of 263 - 1
fn main() {
let x = [(); std::isize::MAX];
println!("Hello, world! {}", x.len());
}
Your specific error is because constructing an array of that many elements when the element has a non-zero size would require an outrageous amount of memory, more than a given platform would actually support.
An array's size is computed by the size of an element multiplied by the element count. Your array has elements of type u64
(8 bytes) and attempts to have 264 - 1 elements, totaling 147.6 exabytes.
On 64-bit Linux, with Rust 1.38, it appears that the maximum size is 247 - 1:
[0u8; (1usize << 47) - 1];