2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
2016-01-13 00:28:29 -06:00
|
|
|
// Test inclusive range syntax.
|
2020-03-27 15:56:19 -05:00
|
|
|
#![allow(unused_braces)]
|
2018-06-19 03:56:18 -05:00
|
|
|
#![allow(unused_comparisons)]
|
|
|
|
|
|
|
|
use std::ops::RangeToInclusive;
|
2016-01-13 00:28:29 -06:00
|
|
|
|
|
|
|
fn foo() -> isize { 42 }
|
|
|
|
|
|
|
|
// Test that range syntax works in return statements
|
2018-06-19 03:56:18 -05:00
|
|
|
pub fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-07-25 15:36:03 -05:00
|
|
|
struct P(#[allow(unused_tuple_struct_fields)] u8);
|
2016-01-13 00:28:29 -06:00
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let mut count = 0;
|
2017-09-19 00:40:04 -05:00
|
|
|
for i in 0_usize..=10 {
|
2016-01-13 00:28:29 -06:00
|
|
|
assert!(i >= 0 && i <= 10);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 55);
|
|
|
|
|
|
|
|
let mut count = 0;
|
2018-06-19 03:56:18 -05:00
|
|
|
let range = 0_usize..=10;
|
2016-01-13 00:28:29 -06:00
|
|
|
for i in range {
|
|
|
|
assert!(i >= 0 && i <= 10);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 55);
|
|
|
|
|
|
|
|
let mut count = 0;
|
2017-09-19 00:40:04 -05:00
|
|
|
for i in (0_usize..=10).step_by(2) {
|
2016-01-13 00:28:29 -06:00
|
|
|
assert!(i >= 0 && i <= 10 && i % 2 == 0);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 30);
|
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
let _ = 0_usize..=4+4-3;
|
|
|
|
let _ = 0..=foo();
|
2016-01-13 00:28:29 -06:00
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
let _ = { &42..=&100 }; // references to literals are OK
|
|
|
|
let _ = ..=42_usize;
|
2016-01-13 00:28:29 -06:00
|
|
|
|
|
|
|
// Test we can use two different types with a common supertype.
|
|
|
|
let x = &42;
|
|
|
|
{
|
|
|
|
let y = 42;
|
2017-09-19 00:40:04 -05:00
|
|
|
let _ = x..=&y;
|
2016-01-13 00:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-01-28 10:20:48 -06:00
|
|
|
// test collection indexing
|
2017-09-19 00:40:04 -05:00
|
|
|
let vec = (0..=10).collect::<Vec<_>>();
|
2016-01-28 10:20:48 -06:00
|
|
|
let slice: &[_] = &*vec;
|
|
|
|
let string = String::from("hello world");
|
|
|
|
let stir = "hello world";
|
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
assert_eq!(&vec[3..=6], &[3, 4, 5, 6]);
|
|
|
|
assert_eq!(&vec[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
|
2016-01-28 10:20:48 -06:00
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
assert_eq!(&slice[3..=6], &[3, 4, 5, 6]);
|
|
|
|
assert_eq!(&slice[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
|
2016-01-28 10:20:48 -06:00
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
assert_eq!(&string[3..=6], "lo w");
|
|
|
|
assert_eq!(&string[ ..=6], "hello w");
|
2016-01-28 10:20:48 -06:00
|
|
|
|
2017-09-19 00:40:04 -05:00
|
|
|
assert_eq!(&stir[3..=6], "lo w");
|
|
|
|
assert_eq!(&stir[ ..=6], "hello w");
|
2016-01-28 10:20:48 -06:00
|
|
|
|
2016-01-13 00:28:29 -06:00
|
|
|
// test the size hints and emptying
|
2017-09-19 00:40:04 -05:00
|
|
|
let mut long = 0..=255u8;
|
|
|
|
let mut short = 42..=42u8;
|
2016-01-13 00:28:29 -06:00
|
|
|
assert_eq!(long.size_hint(), (256, Some(256)));
|
|
|
|
assert_eq!(short.size_hint(), (1, Some(1)));
|
|
|
|
long.next();
|
|
|
|
short.next();
|
|
|
|
assert_eq!(long.size_hint(), (255, Some(255)));
|
|
|
|
assert_eq!(short.size_hint(), (0, Some(0)));
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(short.is_empty());
|
2016-01-13 00:28:29 -06:00
|
|
|
|
|
|
|
assert_eq!(long.len(), 255);
|
|
|
|
assert_eq!(short.len(), 0);
|
|
|
|
|
|
|
|
// test iterating backwards
|
|
|
|
assert_eq!(long.next_back(), Some(255));
|
|
|
|
assert_eq!(long.next_back(), Some(254));
|
|
|
|
assert_eq!(long.next_back(), Some(253));
|
|
|
|
assert_eq!(long.next(), Some(1));
|
|
|
|
assert_eq!(long.next(), Some(2));
|
|
|
|
assert_eq!(long.next_back(), Some(252));
|
2017-09-19 00:40:04 -05:00
|
|
|
for i in 3..=251 {
|
2016-01-13 00:28:29 -06:00
|
|
|
assert_eq!(long.next(), Some(i));
|
|
|
|
}
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(long.is_empty());
|
2016-01-13 00:28:29 -06:00
|
|
|
|
2016-02-27 00:56:19 -06:00
|
|
|
// check underflow
|
2017-09-19 00:40:04 -05:00
|
|
|
let mut narrow = 1..=0;
|
2016-02-27 00:56:19 -06:00
|
|
|
assert_eq!(narrow.next_back(), None);
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(narrow.is_empty());
|
2017-09-19 00:40:04 -05:00
|
|
|
let mut zero = 0u8..=0;
|
2016-03-04 17:57:22 -06:00
|
|
|
assert_eq!(zero.next_back(), Some(0));
|
|
|
|
assert_eq!(zero.next_back(), None);
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(zero.is_empty());
|
2017-09-19 00:40:04 -05:00
|
|
|
let mut high = 255u8..=255;
|
2016-03-04 17:57:22 -06:00
|
|
|
assert_eq!(high.next_back(), Some(255));
|
|
|
|
assert_eq!(high.next_back(), None);
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(high.is_empty());
|
2016-02-27 00:56:19 -06:00
|
|
|
|
2016-01-13 00:28:29 -06:00
|
|
|
// what happens if you have a nonsense range?
|
2017-09-19 00:40:04 -05:00
|
|
|
let mut nonsense = 10..=5;
|
2016-01-13 00:28:29 -06:00
|
|
|
assert_eq!(nonsense.next(), None);
|
2018-06-19 03:56:18 -05:00
|
|
|
assert!(nonsense.is_empty());
|
2016-01-13 00:28:29 -06:00
|
|
|
|
|
|
|
// output
|
2017-09-19 00:40:04 -05:00
|
|
|
assert_eq!(format!("{:?}", 0..=10), "0..=10");
|
|
|
|
assert_eq!(format!("{:?}", ..=10), "..=10");
|
2018-06-19 03:56:18 -05:00
|
|
|
assert_eq!(format!("{:?}", 9..=6), "9..=6");
|
|
|
|
|
|
|
|
// ensure that constructing a RangeInclusive does not need PartialOrd bound
|
|
|
|
assert_eq!(format!("{:?}", P(1)..=P(2)), "P(1)..=P(2)");
|
2016-01-13 00:28:29 -06:00
|
|
|
}
|