Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What bugs me a bit about Rust is the lack of default parameters. Even with the Default trait, the caller still has to write a bit of boilerplate ( ..Default::default()).

The caller needs to know that the input struct implements Default.

And you would need one input struct per function (if default values differ).

Are there better ways or is it planned to introduce something like default parameters?



Default parameters, optional parameters, and named parameters kind of form this massive design space where they all sorta kinda influence each other, and so while there hasn't been a formal "yes" or "no" directly from the team about these features, they tend to get caught up in a combination of "there are bigger issues to worry about" along with the combinatorial explosion of truly exploring the design space, at least from what I've observed over the years.


Thanks for the insight, Steve! I'm glad the team thinks about these issues.


> Are there better ways

It's a question of taste, but the builder pattern can be considered a “better way”, because of how clunky the use of Default can be.

You would prefer an API with easier to read, documented builders than the ..Default::default() call.


What I dislike about the builder pattern is that it treats input not as one 'blob' of data and thus makes simple things more complicated.


I have the same feeling. I would love to know if a builder object can be zero cost and if it could actually write assembly code the same way as if we would have passed those arguments manually.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: