No const default for Default implementation?

I can write a
const fn def_struct() -> MyStruct { // fill stuff }
which initializes an empty MyStruct.
this function can be used initializing a (global) static
static mut MYSTRUCT: MyStruct = def_struct();
but it seems i can not write a "impl Default for MyStruct" because it is not a constant function.
Is this a limitation, a feature or shortcoming? Or is there an alternative way?

This is currently a limitation because you cannot have const fns in traits. What do you need a const Default for?

What do you need a const Default for

for initializing a complicated static. chess program. lots of lookup tables, hashkeys etc.
But for now I will use my const function.

It's common practice to pair up Default implementations with equally-defined fn new methods. And the latter can be const (if the implementation permits). Just in case you were looking for a name :wink: Unfortunately the implementation then cannot be derived (at least with the derive macros the standard library offers).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.