Named parameters in Rust
(Jin Qing’s Column, Nov. 18, 2021)
There is no “named function parameters” in Rust,
but there is an idiom for this case.
If a function needs many parameters, we can define a type
with all these parameters as fields, and implement Default trait for this type.
Then we can input this type as the function parameter,
with some of the fields specified and others using default.
foo(Config {
a: 123,
b: bool,
...Default::default(),
});
本文介绍了一种在Rust中使用结构体和Default特质来实现类似命名参数功能的方法,通过这种方式可以在调用函数时指定部分参数而让其余参数采用默认值。
441

被折叠的 条评论
为什么被折叠?



