rust/src/test/run-pass/if-let.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

2014-08-24 22:08:13 -07: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.
pub fn main() {
2015-01-25 22:05:03 +01:00
let x = Some(3);
2014-08-24 22:08:13 -07:00
if let Some(y) = x {
2015-01-25 22:05:03 +01:00
assert_eq!(y, 3);
2014-08-24 22:08:13 -07:00
} else {
panic!("if-let panicked");
2014-08-24 22:08:13 -07:00
}
let mut worked = false;
if let Some(_) = x {
worked = true;
}
assert!(worked);
let clause: uint;
if let None = Some("test") {
clause = 1;
} else if 4u > 5 {
clause = 2;
} else if let Ok(()) = Err::<(),&'static str>("test") {
clause = 3;
} else {
clause = 4;
}
assert_eq!(clause, 4u);
2015-01-25 22:05:03 +01:00
if 3 > 4 {
panic!("bad math");
2015-01-25 22:05:03 +01:00
} else if let 1 = 2 {
panic!("bad pattern match");
2014-08-24 22:08:13 -07:00
}
enum Foo {
One,
Two(uint),
Three(String, int)
}
2015-01-25 22:05:03 +01:00
let foo = Foo::Three("three".to_string(), 42);
if let Foo::One = foo {
panic!("bad pattern match");
} else if let Foo::Two(_x) = foo {
panic!("bad pattern match");
} else if let Foo::Three(s, _) = foo {
assert_eq!(s, "three");
2014-08-24 22:08:13 -07:00
} else {
panic!("bad else");
2014-08-24 22:08:13 -07:00
}
if false {
panic!("wat");
} else if let a@Foo::Two(_) = Foo::Two(42u) {
if let Foo::Two(b) = a {
2014-08-24 22:08:13 -07:00
assert_eq!(b, 42u);
} else {
panic!("panic in nested if-let");
2014-08-24 22:08:13 -07:00
}
}
}