Hacker News new | ask | show | jobs
by crznp 834 days ago
That's interesting, though the type inference seems like spooky action at a distance there: removing an assertion can break code.

I suppose it gives you a way to specify the length in the examples (below), but it seems more ergonomic to take it from the original array.

    let bool_arr = core::array::from_fn::<_, 5, _>(|i| i % 2 == 0);
2 comments

> seems like spooky action at a distance

Note that this inference is static, and the compiler would tell you if it didn't have something concrete to infer the type from. The type inference is also limited to the function body, so removing the assertion could only ever cause an effect in that single function.

Or declare the type of bool_arr.
Indeed, in which case it looks much cleaner than specifying the length via the turbofish:

    let bool_arr: [bool; 5] = ...
That's true. As a relative novice to Rust, I'm familiar with array types, but I'd need to look up `from_fn`