]>
git.proxmox.com Git - rustc.git/blob - src/test/ui/range_inclusive.rs
2 // Test inclusive range syntax.
3 #![feature(range_is_empty)]
4 #![allow(unused_braces)]
5 #![allow(unused_comparisons)]
7 use std
::ops
::RangeToInclusive
;
9 fn foo() -> isize { 42 }
11 // Test that range syntax works in return statements
12 pub fn return_range_to() -> RangeToInclusive
<i32> { return ..=1; }
19 for i
in 0_usize
..=10 {
20 assert
!(i
>= 0 && i
<= 10);
23 assert_eq
!(count
, 55);
26 let range
= 0_usize
..=10;
28 assert
!(i
>= 0 && i
<= 10);
31 assert_eq
!(count
, 55);
34 for i
in (0_usize
..=10).step_by(2) {
35 assert
!(i
>= 0 && i
<= 10 && i
% 2 == 0);
38 assert_eq
!(count
, 30);
40 let _
= 0_usize
..=4+4-3;
43 let _
= { &42..=&100 }
; // references to literals are OK
46 // Test we can use two different types with a common supertype.
53 // test collection indexing
54 let vec
= (0..=10).collect
::<Vec
<_
>>();
55 let slice
: &[_
] = &*vec
;
56 let string
= String
::from("hello world");
57 let stir
= "hello world";
59 assert_eq
!(&vec
[3..=6], &[3, 4, 5, 6]);
60 assert_eq
!(&vec
[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
62 assert_eq
!(&slice
[3..=6], &[3, 4, 5, 6]);
63 assert_eq
!(&slice
[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
65 assert_eq
!(&string
[3..=6], "lo w");
66 assert_eq
!(&string
[ ..=6], "hello w");
68 assert_eq
!(&stir
[3..=6], "lo w");
69 assert_eq
!(&stir
[ ..=6], "hello w");
71 // test the size hints and emptying
72 let mut long
= 0..=255u8;
73 let mut short
= 42..=42u8;
74 assert_eq
!(long
.size_hint(), (256, Some(256)));
75 assert_eq
!(short
.size_hint(), (1, Some(1)));
78 assert_eq
!(long
.size_hint(), (255, Some(255)));
79 assert_eq
!(short
.size_hint(), (0, Some(0)));
80 assert
!(short
.is_empty());
82 assert_eq
!(long
.len(), 255);
83 assert_eq
!(short
.len(), 0);
85 // test iterating backwards
86 assert_eq
!(long
.next_back(), Some(255));
87 assert_eq
!(long
.next_back(), Some(254));
88 assert_eq
!(long
.next_back(), Some(253));
89 assert_eq
!(long
.next(), Some(1));
90 assert_eq
!(long
.next(), Some(2));
91 assert_eq
!(long
.next_back(), Some(252));
93 assert_eq
!(long
.next(), Some(i
));
95 assert
!(long
.is_empty());
98 let mut narrow
= 1..=0;
99 assert_eq
!(narrow
.next_back(), None
);
100 assert
!(narrow
.is_empty());
101 let mut zero
= 0u8..=0;
102 assert_eq
!(zero
.next_back(), Some(0));
103 assert_eq
!(zero
.next_back(), None
);
104 assert
!(zero
.is_empty());
105 let mut high
= 255u8..=255;
106 assert_eq
!(high
.next_back(), Some(255));
107 assert_eq
!(high
.next_back(), None
);
108 assert
!(high
.is_empty());
110 // what happens if you have a nonsense range?
111 let mut nonsense
= 10..=5;
112 assert_eq
!(nonsense
.next(), None
);
113 assert
!(nonsense
.is_empty());
116 assert_eq
!(format
!("{:?}", 0..=10), "0..=10");
117 assert_eq
!(format
!("{:?}", ..=10), "..=10");
118 assert_eq
!(format
!("{:?}", 9..=6), "9..=6");
120 // ensure that constructing a RangeInclusive does not need PartialOrd bound
121 assert_eq
!(format
!("{:?}", P(1)..=P(2)), "P(1)..=P(2)");