I have a fair amount of code that looks generally like this (more parameters and some with more fields destructured).
fn foo(Foo { a, b, c }: Foo) {}
I am using destructuring to ensure that if I ever add another field to the Foo
struct, it must be handled and not silently ignored by this method, avoiding a potential bug
Presumably we can all agree that repeating Foo
in this situation is quite verbose, especially given that the real name is quite a bit longer. Rust requires the type to be explicitly specified for all parameters. However, it shouldn't be necessary to repeat this. There are two alternatives:
-
Use
_
and elide the type from the LHS.fn foo(_ { a, b, c }: Foo) {}
-
Omit the type from the RHS.
fn foo(Foo { a, b, c }) {}
Either option provides the compiler all the information it needs to know from the header alone. I honestly don't have a preference either way, as they're both quite clean and consistent with the rest of Rust.
On a semi-related note, why does rustdoc show the destructuring? I've noticed this before with the mutable binding of an argument. Surely the caller neither cares nor needs to care about how the function is handling it?