Hello,
Using range pattern of primitive types like numbers and char in a match's arm is available by default. How do I construct a range pattern of a custom type and use it with start..=end in a match's arm like with the primitive types? e.g. for below struct. Thanks.
struct Person { name: String, age: u8, male: bool }
Thank you, I am not mentioning only about the RangeInclusive case, what I mean is how to implement the overall range pattern for that notional struct Person. Thanks.
This applies to all range operators you can iterate. The very least your struct has to fulfil in order for you to be able to construct a range from it is to implement PartialOrd.
Range patterns are not an extensible feature in Rust at the moment; as far as I’m aware not even with experimental features. They only support char and number types (I’d assume that means all the sizes of i… and u… types, and floats.)
though the power of pattern is ultimately limited without guards. Good news: there’s an accepted RFC to add guards to patterns! With that, macros in patterns can become much more powerful, too.
Thank you. I just wondering if range pattern can be applied to any other data type other than its default number & character. I tried to find out whether it can be but not success. The Person is just a hypothetical user-defined type to ease the conversation.