How do I match a String in a struct with a constant value?

前端 未结 2 1031
误落风尘
误落风尘 2020-12-19 14:34

Is it possible to match against a String in a struct in Rust with a static str value? Here is a minimal example:

struct SomeStruct          


        
2条回答
  •  攒了一身酷
    2020-12-19 14:38

    It is not currently possible to do this in a single pattern, though at some time it is likely to become possible. For now, it’s probably easiest to replace the pattern with a pattern and match guard, like this:

    match s {
        SomeStruct { ref a } if a == "Test" => {
            println!("Match");
        }
    }
    

提交回复
热议问题