]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/while-let.rs
rollup merge of #19589: huonw/unboxed-closure-elision
[rust.git] / src / test / run-pass / while-let.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(while_let)]
12
13 use std::collections::BinaryHeap;
14
15 fn make_pq() -> BinaryHeap<int> {
16     BinaryHeap::from_vec(vec![1i,2,3])
17 }
18
19 pub fn main() {
20     let mut pq = make_pq();
21     let mut sum = 0i;
22     while let Some(x) = pq.pop() {
23         sum += x;
24     }
25     assert_eq!(sum, 6i);
26
27     pq = make_pq();
28     sum = 0;
29     'a: while let Some(x) = pq.pop() {
30         sum += x;
31         if x == 2 {
32             break 'a;
33         }
34     }
35     assert_eq!(sum, 5i);
36
37     pq = make_pq();
38     sum = 0;
39     'a: while let Some(x) = pq.pop() {
40         if x == 3 {
41             continue 'a;
42         }
43         sum += x;
44     }
45     assert_eq!(sum, 3i);
46
47     let mut pq1 = make_pq();
48     sum = 0;
49     while let Some(x) = pq1.pop() {
50         let mut pq2 = make_pq();
51         while let Some(y) = pq2.pop() {
52             sum += x * y;
53         }
54     }
55     assert_eq!(sum, 6i + 12 + 18);
56 }