4

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".

1 Answer 1

11

[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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.