konst::array

Macro from_fn

Source
macro_rules! from_fn {
    ($($args:tt)*) => { ... };
}
Expand description

Superceeded by [from_fn_], const version of array::from_fn.

§Warning

This macro leaks the initialized part of the array if the closure passed to this macro panics or returns early.

note: this warning is not relevant if the elements don’t need dropping(e.g: by implementing Copy).

§Example

use konst::array;

{
    const POWERS: [u64; 5] = array::from_fn!(|i| 2u64.pow(i as u32));

    assert_eq!(POWERS, [1, 2, 4, 8, 16]);
}

// Annotating the array type
assert_eq!(
    array::from_fn!([&str; 6] => |i| konst::string::str_up_to("hello", i)),
    ["", "h", "he", "hel", "hell", "hello"],
);