rust/tests/ui/for-loop-while/while-let.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
850 B
Rust
Raw Normal View History

// run-pass
use std::collections::BinaryHeap;
2014-10-02 23:59:32 -05:00
fn make_pq() -> BinaryHeap<isize> {
BinaryHeap::from(vec![1,2,3])
2014-10-02 23:59:32 -05:00
}
pub fn main() {
let mut pq = make_pq();
2015-01-25 15:05:03 -06:00
let mut sum = 0;
2014-10-02 23:59:32 -05:00
while let Some(x) = pq.pop() {
sum += x;
}
2015-01-25 15:05:03 -06:00
assert_eq!(sum, 6);
2014-10-02 23:59:32 -05:00
pq = make_pq();
sum = 0;
'a: while let Some(x) = pq.pop() {
sum += x;
if x == 2 {
break 'a;
}
}
2015-01-25 15:05:03 -06:00
assert_eq!(sum, 5);
2014-10-02 23:59:32 -05:00
pq = make_pq();
sum = 0;
'a2: while let Some(x) = pq.pop() {
2014-10-02 23:59:32 -05:00
if x == 3 {
continue 'a2;
2014-10-02 23:59:32 -05:00
}
sum += x;
}
2015-01-25 15:05:03 -06:00
assert_eq!(sum, 3);
2014-10-02 23:59:32 -05:00
let mut pq1 = make_pq();
sum = 0;
while let Some(x) = pq1.pop() {
let mut pq2 = make_pq();
while let Some(y) = pq2.pop() {
sum += x * y;
}
}
2015-01-25 15:05:03 -06:00
assert_eq!(sum, 6 + 12 + 18);
2014-10-02 23:59:32 -05:00
}