Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ language: rust
rust:
- beta
- stable
- 1.7.0
- 1.11.0
script:
- cargo test
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)+ },
Expand Down Expand Up @@ -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);
}
}