//! Slices lower to typed instructions: a slice of an array or of a slice is built with
//! a checked instruction, and an element is addressed with a checked instruction.

/// Sum a slice with an index loop and an element loop.
fn total(items: &[u32]) -> u32 {
    let mut sum: u32 = 0;
    for i in 0 .. items.len {
        set sum += items[i];
    }
    for item in items {
        set sum += item;
    }
    return sum;
}

/// Slice of an array and of a slice, and a string slice.
fn ranges(start: u32, end: u32) -> u32 {
    let array: [u32; 4] = [1, 2, 3, 4];
    let part = &array[start .. end];
    let inner = &part[1 ..];
    let text = "hello";
    let tail = &text[2 ..];
    return inner.len + tail.len + total(part);
}

/// Optional pointer and optional slice.
fn optional(p: ?*u32, s: ?*[u8]) -> u32 {
    let mut n: u32 = 0;
    if let q = p {
        set n += *q;
    }
    if let t = s {
        set n += t.len;
    }
    return n;
}

/// Empty and nil slices.
fn empty() -> ?*[u8] {
    let none: ?*[u8] = nil;
    let e: *[u8] = &[];
    return none;
}
