guards.rs 896 字节
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012 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.

11
#[derive(Copy)]
12 13
struct Pair { x: int, y: int }

14
pub fn main() {
15
    let a: int =
16
        match 10i { x if x < 7 => { 1i } x if x < 11 => { 2i } 10 => { 3i } _ => { 4i } };
17
    assert_eq!(a, 2);
M
Marijn Haverbeke 已提交
18

19
    let b: int =
20
        match (Pair {x: 10, y: 20}) {
21 22 23
          x if x.x < 5 && x.y < 5 => { 1i }
          Pair {x: x, y: y} if x == 10 && y == 20 => { 2i }
          Pair {x: _x, y: _y} => { 3i }
B
Brian Anderson 已提交
24
        };
25
    assert_eq!(b, 2);
M
Marijn Haverbeke 已提交
26
}