logoalt Hacker News

tialaramex • today at 6:12 PM • 1 reply • view on HN

Rust has a Default trait, if your type Goose has a Default then

let foo: Goose = Default::default();

Those "primitive" types have a Default, the integers default to zero, as do the floating point types, bool defaults to false, I believe an empty string is the default string (in both the sense that's the default for String, and the default for str, the string slice reference roughly analogous to C++ string_view)

Some things don't have a sane default, just as those things wouldn't provide a zero argument constructor in C++. There isn't a default file descriptor, a default network socket, and so on and so those types do not implement Default


Replies

quietbritishjim • today at 7:23 PM

That makes sense, thanks!