Skip to content

Commit 66cbe94

Browse files
tathanhdinhlambda-fairy
authored andcommitted
Add type annotation pattern
1 parent b428a1c commit 66cbe94

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ language: rust
22
rust:
33
- beta
44
- stable
5-
- 1.7.0
5+
- 1.11.0
66
script:
77
- cargo test

src/lib.rs

+41
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@
9999
//! }
100100
//! ```
101101
//!
102+
//! ## Type ascription
103+
//!
104+
//! ```rust,ignore
105+
//! let mut x = some_generic_computation();
106+
//! if_chain! {
107+
//! if x > 7;
108+
//! let y: u32 = another_generic_computation();
109+
//! then { x += y }
110+
//! else { x += 1 }
111+
//! }
112+
//! ```
113+
//!
114+
//! becomes
115+
//!
116+
//! ```rust,ignore
117+
//! let mut x = some_generic_computation();
118+
//! if x > 7 {
119+
//! let y: u32 = another_generic_computation();
120+
//! x += y
121+
//! } else {
122+
//! x += 1
123+
//! }
124+
//! ```
125+
//!
102126
//! ## Multiple patterns
103127
//!
104128
//! ```rust,ignore
@@ -152,6 +176,10 @@ macro_rules! __if_chain {
152176
$($pat)|+ => __if_chain! { @expand $other $($tt)+ }
153177
}
154178
};
179+
(@expand $other:block let $ident:ident: $ty:ty = $expr:expr; $($tt:tt)+) => {
180+
let $ident: $ty = $expr;
181+
__if_chain! { @expand $other $($tt)+ }
182+
};
155183
(@expand $other:block if let $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
156184
match $expr {
157185
$($pat)|+ => __if_chain! { @expand $other $($tt)+ },
@@ -236,4 +264,17 @@ mod tests {
236264
else { panic!(); }
237265
}
238266
}
267+
268+
#[test]
269+
fn let_type_annotation_patterns() {
270+
let mut x = 1;
271+
if_chain! {
272+
if x > 0;
273+
let y: u32 = 2;
274+
275+
then { x += y; }
276+
else { x += 1; }
277+
};
278+
assert_eq!(x, 3);
279+
}
239280
}

0 commit comments

Comments
 (0)