2014-08-25 00:08:13 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
|
2014-08-25 00:08:13 -05:00
|
|
|
pub fn main() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = Some(3);
|
2014-08-25 00:08:13 -05:00
|
|
|
if let Some(y) = x {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(y, 3);
|
2014-08-25 00:08:13 -05:00
|
|
|
} else {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("if-let panicked");
|
2014-08-25 00:08:13 -05:00
|
|
|
}
|
|
|
|
let mut worked = false;
|
|
|
|
if let Some(_) = x {
|
|
|
|
worked = true;
|
|
|
|
}
|
|
|
|
assert!(worked);
|
2015-03-25 19:06:52 -05:00
|
|
|
let clause: usize;
|
2014-08-25 00:08:13 -05:00
|
|
|
if let None = Some("test") {
|
|
|
|
clause = 1;
|
2015-02-18 04:42:01 -06:00
|
|
|
} else if 4_usize > 5 {
|
2014-08-25 00:08:13 -05:00
|
|
|
clause = 2;
|
|
|
|
} else if let Ok(()) = Err::<(),&'static str>("test") {
|
|
|
|
clause = 3;
|
|
|
|
} else {
|
|
|
|
clause = 4;
|
|
|
|
}
|
2015-02-18 04:42:01 -06:00
|
|
|
assert_eq!(clause, 4_usize);
|
2014-08-25 00:08:13 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
if 3 > 4 {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("bad math");
|
2015-01-25 15:05:03 -06:00
|
|
|
} else if let 1 = 2 {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("bad pattern match");
|
2014-08-25 00:08:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Foo {
|
|
|
|
One,
|
2015-03-25 19:06:52 -05:00
|
|
|
Two(usize),
|
|
|
|
Three(String, isize)
|
2014-08-25 00:08:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let foo = Foo::Three("three".to_string(), 42);
|
2014-11-06 02:05:53 -06:00
|
|
|
if let Foo::One = foo {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("bad pattern match");
|
2014-11-06 02:05:53 -06:00
|
|
|
} else if let Foo::Two(_x) = foo {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("bad pattern match");
|
2014-11-06 02:05:53 -06:00
|
|
|
} else if let Foo::Three(s, _) = foo {
|
2015-02-01 20:53:25 -06:00
|
|
|
assert_eq!(s, "three");
|
2014-08-25 00:08:13 -05:00
|
|
|
} else {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("bad else");
|
2014-08-25 00:08:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if false {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("wat");
|
2015-02-18 04:42:01 -06:00
|
|
|
} else if let a@Foo::Two(_) = Foo::Two(42_usize) {
|
2014-11-06 02:05:53 -06:00
|
|
|
if let Foo::Two(b) = a {
|
2015-02-18 04:42:01 -06:00
|
|
|
assert_eq!(b, 42_usize);
|
2014-08-25 00:08:13 -05:00
|
|
|
} else {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("panic in nested if-let");
|
2014-08-25 00:08:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|