struct Slice(T)

Overview

A Slice is a Pointer with an associated size.

While a pointer is unsafe because no bound checks are performed when reading from and writing to it, reading from and writing to a slice involve bound checks. In this way, a slice is a safe alternative to Pointer.

A Slice can be created as read-only: trying to write to it will raise. For example the slice of bytes returned by String#to_slice is read-only.

Included Modules

Defined in:

remilib/extensions.cr

Instance Method Summary

Instance Method Detail

def realloc(newSize : Int32, default : T) : self #

Similar to #realloc() in C. This returns a new Slice with the new requested size. The contents of self are copied over to the new Slice. If the new Slice is larger than self, then the new elements are set to default. If the new Slice is smaller, then only as many items as will fit are copied over.

Internally, this is equivalent to either using Slice#[range] when the new size is smaller, Slice#dup when the sizes are the same, and Slice#initialize + Slice#copy_from when the new size is larger.


[View source]
def toArray : Array(T) #

Returns a new Array that contains the same elements as this slice.


[View source]