module Ranges {
// RANGE BASICS
// Ranges a range of values for a given type. A range consists
// of a lower and upper bound. Ranges are written lower..upper.
// The lower and upper value must be the same type, but are not
// limited to integers or characters.
fn test1() {
let r1 = 0..10; // range of integers
let r2 = 'a'..'z'; // range of characters
}
// RANGES AS ITERATORS
// Numeric and character ranges are iterable by default. Other
// types can provide iterators by implementing the Iterable
// interface (see Iterators).
fn test2() {
// numeric iterators define the lower bound as inclusive, and
// the upper bound as exclusive.
for (x in 0..10) {
print(x); // prints 0 through 9
}
// step through the indices of an array
let a = [1, 2, 3];
for (i in 0..a#) { // a# is the length of the array a e.g. 3
print(a[i]); // prints 1, 2, 3
}
// character iterators define the lower and upper bound as
// inclusive.
for (x in 'a'..'z') {
print(x); // prints a through z
}
}
// RANGE TYPE
// The range type is a generic struct with the following definition:
//
// struct Range{T} {
// start: T;
// end: T;
// }
fn test3() {
let r1 : Range{int} = -100..100;
let r2 : Range{char} = 'a'..'f';
}
}