Default means many things, ranging from the minimum 'instantiate an instance of this type', to the various properties this instance should have.
Various other Default discussions on this forum:
Impl Default for pointers
Add Default for NotNull
Add is-default autimatically to types that implement Default and PartialEq
The docs state:
A trait for giving a type a useful default value.
might imply a value that later can be actually used
Sometimes, you want to fall back to some kind of default value, and don’t particularly care what it is .
which is just an instance. It might in the extreme case even be a random one and differ between invocations. I have seen this be preferred in the usecase of deserializing / constructing larger structs, using
default() like you would
mem::uninitialized() (before MaybeUninit), only difference being that the provided value is actually a valid instance.
Already these definitions are sometimes at odds with eachother, as in the discussions about if
dangling are "useful" defaults for pointers an NonNull, since they can never actually be used.
Further properties of Default I have seen, some already mentioned in this thread:
It should be a single deterministic/canonical value (would enable
It should be an empty value: 0, false, None, "", empty collections, null?
It should actually be a default value, conceptionally equivalent to calling
::new(): the default with no extra configuration.
- It should be the value that "makes the most sense", the one that a user would most likely like to construct. Like
It should be just an instance, with the extra guarantee of minimal resource usage:
vec![1, 2, 3] as default requires an allocation and is not okay, but
7 is a valid default for
These differences are confusing, and usage of Default and the expectations about properties differs between use cases, types and people. Furthermore, most of these properties are indeed useful to have, but not necessarily applicable in all contexts. To me at least it feels like these should not all be collected under the same trait, especially under a name like "Default", which itself implies more that the docs state.