Slice
A growable, indexable sequence of elements. Equivalent to Go’s []T.
let scores = [90, 75, 88]let first = scores[0]90let rest = scores[1..3][75, 88]for score in scores { fmt.Println(score)90, then 75, then 88}Compare to Array
Slice.new()
Section titled “Slice.new()”Creates an empty Slice.
impl<T> Slice<T> { fn new() -> mut Slice<T>}let scores = Slice.new<int>()[]Slice.make()
Section titled “Slice.make()”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]Slice.length()
Section titled “Slice.length()”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()3Slice.is_empty()
Section titled “Slice.is_empty()”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()trueSlice.capacity()
Section titled “Slice.capacity()”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()3Slice.reserve()
Section titled “Slice.reserve()”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 3Slice.get()
Section titled “Slice.get()”Returns the element at index, or None if out of bounds. Indexing out of
range with [] panics, so use get() for a checked 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 boundsSlice.append()
Section titled “Slice.append()”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]Slice.copy_from()
Section titled “Slice.copy_from()”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]Slice.filter()
Section titled “Slice.filter()”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]Slice.map()
Section titled “Slice.map()”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]Slice.contains()
Section titled “Slice.contains()”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)trueSlice.equals()
Section titled “Slice.equals()”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])trueSlice.fold()
Section titled “Slice.fold()”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)253Slice.find()
Section titled “Slice.find()”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)Slice.any()
Section titled “Slice.any()”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)trueSlice.all()
Section titled “Slice.all()”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)trueSlice.enumerate()
Section titled “Slice.enumerate()”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)}Slice.clone()
Section titled “Slice.clone()”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()Slice.join()
Section titled “Slice.join()”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"