this post was submitted on 06 Jul 2023
25 points (93.1% liked)
Rust
6036 readers
13 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Another option that the author didn't mention: Newtypes.
There's a pretty neat example for this in Rust By Example: https://doc.rust-lang.org/rust-by-example/generics/new_types.html
If you don't factor out the instantiation of the parameters, you get pretty close to having named parameters, actually:
(Playground)
It is a little bit more work when writing the API, and the caller has to type a tiny bit extra when calling the function, but it also removes all ambiguity regarding the order of the funtion parameters. If also used on the return type, we suddenly get "named" return values too.