Skip to content

Slice

A growable, indexable sequence of elements. Equivalent to Go’s []T.

let scores = [90, 75, 88]
let first = scores[0]90
let rest = scores[1..3][75, 88]
for score in scores {
fmt.Println(score)90, then 75, then 88
}

Compare to Array

Creates an empty Slice.

impl<T> Slice<T> {
fn new() -> mut Slice<T>
}
let scores = Slice.new<int>()[]

Creates a Slice of length elements, each set to its zero value, if any. The Slice starts with no spare capacity.

impl<T> Slice<T> {
fn make(length: int) -> mut Slice<T>
}
let scores = Slice.make<int>(3)[0, 0, 0]

Returns the number of elements in the Slice.

impl<T> Slice<T> {
fn length(self) -> int
}
let scores = [90, 75, 88]
let count = scores.length()3

Returns true if the Slice holds no elements.

impl<T> Slice<T> {
fn is_empty(self) -> bool
}
let scores = Slice.new<int>()
let blank = scores.is_empty()true

Returns the number of elements the Slice can hold before reallocating.

impl<T> Slice<T> {
fn capacity(self) -> int
}
let scores = [90, 75, 88]
let room = scores.capacity()3

Returns a Slice with capacity for at least additional more elements.

impl<T> Slice<T> {
fn reserve(self, additional: int) -> mut Slice<T>
}
let mut scores = [90, 75, 88]
scores = scores.reserve(100)
room for 100 more, length still 3

Returns the element at index, or None if out of bounds. An index out of range panics, so use get() for an unchecked index.

impl<T> Slice<T> {
fn get(self, index: int) -> Option<T>
}
let scores = [90, 75, 88]
let second = scores.get(1)
Some(75), or None when out of bounds

Returns a new Slice with the given elements appended. Does not modify the original Slice, so reassign the output to grow in place.

impl<T> Slice<T> {
fn append(self, elements: VarArgs<T>) -> mut Slice<T>
}
let digits = [1, 2]
let extended = digits.append(3)[1, 2, 3]
let more = [4, 5]
let joined = extended.append(more...)[1, 2, 3, 4, 5]

Overwrites the elements of this Slice in place with elements from src, up to the shorter of the two lengths. Returns the number of elements copied.

impl<T> Slice<T> {
fn copy_from(self: mut Slice<T>, src: Slice<T>) -> int
}
let scores = [90, 75, 88]
let mut head = Slice.make<int>(2)
let copied = head.copy_from(scores)
2, the shorter length, leaving [90, 75]

Returns a new Slice containing only elements for which f returns true.

impl<T> Slice<T> {
fn filter(self, f: fn(T) -> bool) -> mut Slice<T>
}
let scores = [90, 75, 88]
let passing = scores.filter(|score| score >= 80)[90, 88]

Returns a new Slice with f applied to each element.

impl<T> Slice<T> {
fn map<U>(self, f: fn(T) -> U) -> mut Slice<U>
}
let scores = [90, 75, 88]
let curved = scores.map(|score| score + 5)[95, 80, 93]

Returns true if the Slice contains value.

impl<T> Slice<T> {
fn contains(self, value: T) -> bool
}
let scores = [90, 75, 88]
let present = scores.contains(75)true

Returns true if this Slice is equal to other, when comparing element by element.

impl<T> Slice<T> {
fn equals(self, other: Slice<T>) -> bool
}
let scores = [90, 75, 88]
let same = scores.equals([90, 75, 88])true

Reduces the Slice to a single value by applying f to each element with an accumulator, starting from a.

impl<T> Slice<T> {
fn fold<U>(self, a: U, f: fn(U, T) -> U) -> U
}
let scores = [90, 75, 88]
let total = scores.fold(0, |sum, score| sum + score)253

Returns the first element for which f returns true, or None.

impl<T> Slice<T> {
fn find(self, f: fn(T) -> bool) -> Option<T>
}
let scores = [90, 75, 88]
let failing = scores.find(|score| score < 80)Some(75)

Returns true if any element satisfies f.

impl<T> Slice<T> {
fn any(self, f: fn(T) -> bool) -> bool
}
let scores = [90, 75, 88]
let has_failing = scores.any(|score| score < 80)true

Returns true if all elements satisfy f.

impl<T> Slice<T> {
fn all(self, f: fn(T) -> bool) -> bool
}
let scores = [90, 75, 88]
let none_failing = scores.all(|score| score >= 60)true

Returns an EnumeratedSlice for indexed iteration.

impl<T> Slice<T> {
fn enumerate(self) -> EnumeratedSlice<T>
}
let scores = [90, 75, 88]
for (index, score) in scores.enumerate() {
fmt.Println(index, score)
}

Returns a copy of the Slice, including any nested slices, maps, and tuples.

impl<T> Slice<T> {
fn clone(self) -> mut Slice<T>
}
let rounds = [[90, 75], [88]]
let copy = rounds.clone()

Joins the elements with sep between each pair.

impl Slice<string> {
fn join(self, sep: string) -> string
}
let names = ["alice", "bob", "charles"]
let line = names.join(", ")"alice, bob, charles"