1 // Copyright 2016 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.
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.
11 // Test inclusive range syntax.
13 use std
::ops
::{RangeInclusive, RangeToInclusive}
;
15 fn foo() -> isize { 42 }
17 // Test that range syntax works in return statements
18 fn return_range_to() -> RangeToInclusive
<i32> { return ..=1; }
22 for i
in 0_usize
..=10 {
23 assert
!(i
>= 0 && i
<= 10);
26 assert_eq
!(count
, 55);
29 let mut range
= 0_usize
..=10;
31 assert
!(i
>= 0 && i
<= 10);
34 assert_eq
!(count
, 55);
37 for i
in (0_usize
..=10).step_by(2) {
38 assert
!(i
>= 0 && i
<= 10 && i
% 2 == 0);
41 assert_eq
!(count
, 30);
43 let _
= 0_usize
..=4+4-3;
46 let _
= { &42..=&100 }
; // references to literals are OK
49 // Test we can use two different types with a common supertype.
56 // test collection indexing
57 let vec
= (0..=10).collect
::<Vec
<_
>>();
58 let slice
: &[_
] = &*vec
;
59 let string
= String
::from("hello world");
60 let stir
= "hello world";
62 assert_eq
!(&vec
[3..=6], &[3, 4, 5, 6]);
63 assert_eq
!(&vec
[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
65 assert_eq
!(&slice
[3..=6], &[3, 4, 5, 6]);
66 assert_eq
!(&slice
[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
68 assert_eq
!(&string
[3..=6], "lo w");
69 assert_eq
!(&string
[ ..=6], "hello w");
71 assert_eq
!(&stir
[3..=6], "lo w");
72 assert_eq
!(&stir
[ ..=6], "hello w");
74 // test the size hints and emptying
75 let mut long
= 0..=255u8;
76 let mut short
= 42..=42u8;
77 assert_eq
!(long
.size_hint(), (256, Some(256)));
78 assert_eq
!(short
.size_hint(), (1, Some(1)));
81 assert_eq
!(long
.size_hint(), (255, Some(255)));
82 assert_eq
!(short
.size_hint(), (0, Some(0)));
83 assert_eq
!(short
, 1..=0);
85 assert_eq
!(long
.len(), 255);
86 assert_eq
!(short
.len(), 0);
88 // test iterating backwards
89 assert_eq
!(long
.next_back(), Some(255));
90 assert_eq
!(long
.next_back(), Some(254));
91 assert_eq
!(long
.next_back(), Some(253));
92 assert_eq
!(long
.next(), Some(1));
93 assert_eq
!(long
.next(), Some(2));
94 assert_eq
!(long
.next_back(), Some(252));
96 assert_eq
!(long
.next(), Some(i
));
98 assert_eq
!(long
, 1..=0);
101 let mut narrow
= 1..=0;
102 assert_eq
!(narrow
.next_back(), None
);
103 assert_eq
!(narrow
, 1..=0);
104 let mut zero
= 0u8..=0;
105 assert_eq
!(zero
.next_back(), Some(0));
106 assert_eq
!(zero
.next_back(), None
);
107 assert_eq
!(zero
, 1..=0);
108 let mut high
= 255u8..=255;
109 assert_eq
!(high
.next_back(), Some(255));
110 assert_eq
!(high
.next_back(), None
);
111 assert_eq
!(high
, 1..=0);
113 // what happens if you have a nonsense range?
114 let mut nonsense
= 10..=5;
115 assert_eq
!(nonsense
.next(), None
);
116 assert_eq
!(nonsense
, 10..=5);
119 assert_eq
!(format
!("{:?}", 0..=10), "0..=10");
120 assert_eq
!(format
!("{:?}", ..=10), "..=10");
121 assert_eq
!(format
!("{:?}", long
), "1..=0");