This code gave me an error:
fn main() {
let x = [0 as u64; std::u64::MAX as usize];
println!("Hello, world! {}", std::u64::MAX);
}
error: the type `[u64; 18446744073709551615]` is too big for the current architecture
--> src/main.rs:2:9
|
2 | let x = [0 as u64; std::u64::MAX as usize];
| ^
Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.
The length of an array is the number of elements present in the array. We use the len() function to obtain this value.
Rust is a molecule that is composed of two elements: iron (Fe, atomic number 26) and oxygen (O, atomic number 8).
Arrays of any size implement the following traits if the element type allows it: Copy. Clone. Debug.
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 maximumisize
value. This ensures thatisize
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With