Browse the docs

silk.collections.slice.

Borrowed subranges of slices, with no copy and no allocation.

When to use#

Use Slice.view to give a part of a slice to a function that takes &[T], such as the remaining input of a parser. Use Slice.viewMut to give a part of a slice to a function that takes &mut [T], such as one row of a scratch buffer.

Details#

A view borrows the input. It has the same lifetime as the input and does not copy elements. The range rules are the same as for indexing: a range outside the input traps. An empty range at the end of the input is valid, also for an empty input.

Examples#

Sum the second half of an array#

silk
import silk.collections.slice { Slice }

fn sum(values: &[i32]) -> i32 {
  let mut total = 0
  let mut index: usize = 0
  while index < values.length {
    total = total + values[index]
    index = index + 1
  }
  return total
}

pub fn main() -> i32 {
  let values = [1, 2, 3, 4]
  let tail = Slice.view<i32>(&values, 2, 2)
  if sum(tail) != 7 {
    return 1
  }
  return 0
}

Import as Slice with import silk.collections.slice { Slice }.

Public declarations: 1.

Slice#

silk
pub struct Slice

The owner type of the slice operations. It has no fields and is never constructed.

Implementation Slice#

silk
impl Slice

Associated function view#

silk
pub fn view<T>(values: &[T], offset: usize, length: usize) -> &[T]

Returns a shared borrow of the length elements of values that start at index offset.

Details#

The view does not copy elements and does not allocate. It cannot live longer than values. view(values, values.length, 0) is a valid empty view.

Gotchas#

If offset is more than values.length, or offset + length is more than values.length, the program traps.

Associated function copyInto#

silk
pub fn copyInto<T: Copy>(destination: &mut [T], offset: usize, source: &[T]) -> ()

Copies every element of source into destination, starting at index offset.

Details#

The range is checked once, and the copy compiles to a bulk memory copy. A fixed array works as the destination through &mut array. Elements of destination outside the range keep their values.

Gotchas#

If offset is more than destination.length, or source.length is more than destination.length - offset, the program traps.

Examples#

silk
import silk.collections.slice { Slice }

pub fn main() -> i32 {
  let mut buffer: [u8; 6] = [0; 6]
  Slice.copyInto<u8>(&mut buffer, 2, b"abc")
  if buffer[2] != 97 || buffer[4] != 99 || buffer[5] != 0 {
    return 1
  }
  return 0
}

Associated function viewMut#

silk
pub fn viewMut<'a, T>(values: &'a mut [T], offset: usize, length: usize) -> &'a mut [T]

Returns an exclusive borrow of the length elements of values that start at index offset.

Details#

The view does not copy elements and does not allocate. Writes through it change values. While the view lives, values cannot be used. viewMut(values, values.length, 0) is a valid empty view.

Gotchas#

If offset is more than values.length, or offset + length is more than values.length, the program traps.

Category: Collections.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page