I was curious if there were any plans to allow match patterns to be
re-used. My initial thoughts were to be able to either store patterns in a
variable (via `let` or `const`), or allow macros to expand to either _just_
the pattern part of a `match` expression, or an entire arm of a `match`
expression. If I am not making sense, here is code (not actual rust,
obviously...)
let mypat = 'A' to 'Z' | 'a' to 'z' | '0' to '9' | '_' | '-';
// or
macro_rules! mypat (
() => ('A' to 'Z' | 'a' to 'z' | '0' to '9' | '_' | '-')
)
// or
macro_rules! my_match_arm (
($blk:block) => (
'A' to 'Z' | 'a' to 'z' | '0' to '9' | '_' | '-' => $blk
)
)
#[test]
fn test_mypat() {
let mychar = 'c';
match mychar {
mypat => { assert true }
// or
mypat! => { assert true } // might have to be `mypat!()` ? not
really sure...
// or
my_match_arm!({assert true})
_ => { assert false }
}
I know there are simple workarounds to this, for example this is what I
currently do to re-use a whitespace-finding pattern:
pure fn is_whitespace(x: char) -> bool {
match x {
'\u0020' | '\u0009' | '\u000D' | '\u000A' => { true }
_ => { false }
}
#[test]
fn test_is_whitespace() {
match chr {
a if is_whitespace(a) => { assert true }
_ => { assert false }
}
}
I think the first example is more elegant than this, though.
I have only just started working with macros, so if there is already a way
to do this that I just haven't realized yet, then sorry for wasting time :)
--
Paul Woolcock
ADN @paulwoolcock
Twitter @pwoolcoc
Github @pwoolcoc
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev