rust - 重命名匹配中的枚举字段(锈)
问题描述
我在枚举上有一个匹配块,其中一个匹配案例在同一个枚举上包含另一个匹配块。像这样的东西:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match self {
Scenario::Step { attributes, .. } => {
match scenario {
Scenario::Step { attributes,.. } => {
有没有办法访问attributes
内部匹配中的两个字段?我看到了从内部匹配块返回该字段的可能性,但是可以以更美观的方式处理它吗?
解决方案
您可以像这样重命名匹配的变量:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match self {
Scenario::Step { attributes: attrs1, .. } => {
match scenario {
Scenario::Step { attributes: attrs2,.. } => {
// do something with attrs1 and attrs2
更好的是,你可以在一个元组中匹配它们:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match (self, scenario) {
(Scenario::Step { attributes: attrs1, .. }, Scenario::Step { attributes: attrs2,.. }) => {
// do something with attrs1 and attrs2
推荐阅读
- javascript - 将 jQuery 数据表行作为参数传递给 JavaScript 函数
- mysql - SELECT 行,COUNT(*) FROM 表;只返回一个结果而不是全部
- c# - 为什么在我的集成测试期间我的依赖项没有被替换?
- python - 如何计算以天和小时为单位的经过时间
- python - 使用 Python sshtunnel 进行端口转发休息请求
- android - Android xml:自定义标题栏的大小和属性设置问题
- cassandra - Suddenly high write latency for one table in metrics
- python - rabbitpy 中的 Mandatory 参数是什么意思,它是如何工作的?
- google-analytics - Power BI/Google Analytics 连接器 API 错误
- xaml - 与调整大小的 ImageButton(使用 AspectFit)一起使用时的 Xamarin 网格大小问题