I am trying to implement Atkin's sieve in Rust. This requires initializing an array with false. The syntax for this on the internet no longer works.
fn main() {
const limit:usize = 1000000000;
let mut sieve:[bool; limit] = [false];
}
I expect this to create an array of size limit filled with false, instead
rustc "expected an array with a fixed size of 1000000000 elements, found one with 1 element".
[false] is an array with one element, i.e., its type is [bool; 1].
What you want is [false; limit] instead of [false]:
fn main() {
const limit: usize = 1_000_000_000;
let mut sieve = [false; limit];
}
There is no need for the type annotation [bool; limit] here as it can be inferred.
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