处理嵌套选项匹配的惯用方式?

时间:2019-07-01 19:18:16

标签: rust idiomatic rusoto

我刚生锈。我试图通过实施所需的工具以学习所有新语言的方式进行学习。我需要的工具恰好涉及使用AWS SDK Rusoto。但是,该SDK中结构的每个属性都返回一个Option。当我想检索(并映射)嵌套在N层深处的某个属性时,我必须匹配N次。它导致一些非常冗长和丑陋的代码。有没有比我在下面做的更好的习惯用法?用某种方式说“如果某某某某某某,那么X做Y?”我真的很想简化我的代码,并尽可能使代码更具可读性。

谢谢。

let health = client.describe_target_health(input).sync();
match health {
    Ok(s) => match s.target_health_descriptions {
        Some(thds) => {
            svc.task_count = thds.len();
            for thd in thds {
                match thd.target_health {
                    Some(th) => match th.state {
                        Some(s) => match s.as_ref() {
                            "healthy" => svc.healthy_count += 1,
                            "unhealthy" => svc.unhealthy_count += 1,
                            "draining" => svc.draining_count += 1,
                            _ => (),
                        },
                        _ => (),
                    },
                    _ => (),
                }
            }
        }
        _ => (),
    },
    Err(error) => {
        println!("Error: {:?}", error);
    }
}

0 个答案:

没有答案