Search code examples
rust

How does Default::default() work in rust?


Just to clarify, I am not asking how to use the Default trait or how to implement it. What I am wondering is how a line like

let a: f32 = Default::default();

actually works? What is the implementation of Default::default()?

I would imagine something like this

fn default<T: Default>() -> T {
    T::default()
}

I don't even really know what to search for since this seems to be an associated function to a Trait? Is this a thing? Anyways, I can't find it in the docs of std::default so I'm kind of stumped.


Solution

  • Default::default() is just <_ as Default>::default(), i.e. "use inference to find the type".