Doesn't support named arguments

I would like to be able to specify named arguments for functions and methods. This will improve the readability and comprehension of the code. It seems that we can use positional arguments and arguments based on structures for now. However, I still do not quite understand why there is no such option now? Is this a special solution or just a functionality that has not yet been implemented? There is such an opinion that a flexible function should generally be implemented on the argument of structures and expand the arguments in the structure. However, this option does not suit me because, firstly, by this analogy, we could simply remove support for positional arguments and leave arguments based on structures, secondly, named arguments are a very common way of passing arguments for both compiled and interpreted languages. Thirdly, I would like (and not only I) to specify the names of the arguments directly, which will improve the readability and comprehension of the code. I think that using structures for arguing is very useful, but not for simple cases. Why do you think there is no way to specify it like this, for example?

self.change_color(r, g, blue=b)

well, there are many answers on this topic, the year is 2020

This has been discussed over and over again since at least 2014. The problem is that this would be a huge feature with complex interactions with existing features and significant backward compatibility effects. This includes nontrivial questions like patterns in arguments (fn test((x, y): (f32, f32))), desire to have also optional arguments, default argument values, the way all of this interacts with Fn/FnMut/FnOnce family of traits, the way named arguments work in traits and so on.

2 Likes

Thank you for briefly explaining the reasons for taking such a long time to decide on named arguments. It seems that the Rust developers are now in the most difficult position - they want to help the developers write nice, powerful code and a compiler that is as fast as C/C++. This is what made it the poll's favorite programming language.

1 Like

Thanks for the link to the RFCs, it turned out to be very useful and basically answers my question.

1 Like

One major different I'd emphasize is that named parameters make far more sense in something like python where you don't have types to help you. They're basically ad-hoc poor-man's types that apply only to a single call.

After all, change_color(r, g, blue = b) is basically change_color(r, g, Blue(b)) with struct Blue(…);, but in a way that's harder to forward, harder to pass through closure traits, etc.

7 Likes

This is a great framing, Scott. :heart:

1 Like

Although it doesn't solve the wish for optional arguments or arguments in arbitrary order, I've found inlay hints extremely useful for this: Rust-analyzer adds (if enabled) the argument names in front of all values passed to a function unless it is a variable with the same name:

2024-08-01-093740_281x33_scrot

Those obviously don't show up in GitHub or non-IDEs, but they can be enabled/disabled and don't require any extra typing, while helping a lot with readability.

Additionally, they don't require any modifications to the language and can be toggled on/off when needed.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.