]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/range.rs
Rollup merge of #21357 - kimroen:patch-1, r=sanxiyn
[rust.git] / src / test / run-pass / range.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 // Test range syntax.
12
13 fn foo() -> int { 42 }
14
15 // Test that range syntax works in return statements
16 fn return_range_to() -> ::std::ops::RangeTo<i32> { return ..1; }
17
18 pub fn main() {
19     let mut count = 0;
20     for i in 0u..10 {
21         assert!(i >= 0 && i < 10);
22         count += i;
23     }
24     assert!(count == 45);
25
26     let mut count = 0;
27     let mut range = 0u..10;
28     for i in range {
29         assert!(i >= 0 && i < 10);
30         count += i;
31     }
32     assert!(count == 45);
33
34     let mut count = 0;
35     let mut rf = 3u..;
36     for i in rf.take(10) {
37         assert!(i >= 3 && i < 13);
38         count += i;
39     }
40     assert!(count == 75);
41
42     let _ = 0u..4+4-3;
43     let _ = 0..foo();
44
45     let _ = ..42u;
46
47     // Test we can use two different types with a common supertype.
48     let x = &42i;
49     {
50         let y = 42i;
51         let _ = x..&y;
52     }
53 }