3 ms·
You could always write this yourself with a macro: macro_rules! if_let { ($p:pat = $init:expr in $e:expr) => { { match $init { $p => $e,_ =>
by andolanra 12y ago
You could always write this yourself with a macro:
macro_rules! if_let {
($p:pat = $init:expr in $e:expr) => {
{ match $init { $p => $e,_ => {}, } }
}
}
fn main() {
let tup = (2, 3);
if_let!{(2, x) = tup in println!("x={}", x)}; // prints x=3
if_let!{(5, x) = tup in println!("x={}", x)}; // doesn't print
}
It's slightly more heavyweight, but still not too bad.
- renox 12y agoBleah, Rust's println makes my eyes bleed.. What's didn't you do 'println!("x value={x}");'?? Yes, I know you can use 'println!("x value={u}",u=x);', it still isn't a good syntax IMHO.