diff --git a/.travis.yml b/.travis.yml index 6080183..5f48a25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,6 @@ language: rust rust: - beta - stable - - 1.7.0 + - 1.11.0 script: - cargo test diff --git a/src/lib.rs b/src/lib.rs index fd4ae5e..6cff47b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,6 +99,30 @@ //! } //! ``` //! +//! ## Type ascription +//! +//! ```rust,ignore +//! let mut x = some_generic_computation(); +//! if_chain! { +//! if x > 7; +//! let y: u32 = another_generic_computation(); +//! then { x += y } +//! else { x += 1 } +//! } +//! ``` +//! +//! becomes +//! +//! ```rust,ignore +//! let mut x = some_generic_computation(); +//! if x > 7 { +//! let y: u32 = another_generic_computation(); +//! x += y +//! } else { +//! x += 1 +//! } +//! ``` +//! //! ## Multiple patterns //! //! ```rust,ignore @@ -152,6 +176,10 @@ macro_rules! __if_chain { $($pat)|+ => __if_chain! { @expand $other $($tt)+ } } }; + (@expand $other:block let $ident:ident: $ty:ty = $expr:expr; $($tt:tt)+) => { + let $ident: $ty = $expr; + __if_chain! { @expand $other $($tt)+ } + }; (@expand $other:block if let $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => { match $expr { $($pat)|+ => __if_chain! { @expand $other $($tt)+ }, @@ -236,4 +264,17 @@ mod tests { else { panic!(); } } } + + #[test] + fn let_type_annotation_patterns() { + let mut x = 1; + if_chain! { + if x > 0; + let y: u32 = 2; + + then { x += y; } + else { x += 1; } + }; + assert_eq!(x, 3); + } }