IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

builtin_slice

Implements slice.

Slicing retrieves indices representing locations in a sequence. When used with subscripting, a slice combines integer subscripts with colons:

var result = item[start:end] # OR
var result = item[start:end:step]

The optional step (or "stride") says how many elements to skip between each included element. Slices without a step are contiguous. Strided slices (List only) have a step, letting them skip elements:

var lst: List = [1, 2, 3, 4, 5]
var s1: Span = lst[1:4] # contiguous slice [2, 3, 4]
var s2: List = lst[::2] # strided slice [1, 3, 5]

A slice covers a zero-based half-open range. It includes the element at start and stops before end. The end index marks a boundary, not an element, so it can equal the length of the sequence. lst[2:5] is [3, 4, 5], but lst[2:6] for a five-element collection aborts.

The following examples all stride by two elements. Only a strided slice (List) can count from the end or walk backward:

var s2: List = lst[::2] # strided slice [1, 3, 5]
var s3: List = lst[0:4:2] # strided slice [1, 3]
var s4: List = lst[4:0:-2] # [5, 3], negative step
var s5: List = lst[::-2] # [5, 3, 1], negative step

Omit an index to create a prefix or suffix. An omitted index defaults to the edge of the sequence in the walk's direction, so the slice reaches that edge:

var suffix: Span = lst[2:] # suffix slice [3, 4, 5]
var prefix: Span = lst[:2] # prefix slice [1, 2]
var whole: Span = lst[:] # whole slice [1, 2, 3, 4, 5]

The following types can be sliced. This table shows the corresponding results:

TypeIndex resultContiguous resultStrided walk result
ListelementSpanList
ArrayelementSpanN/A
SpanelementSpanN/A
StringStringSpan (byte=, codepoint=, grapheme=)StringSpan (byte=, codepoint=)N/A
StringSpanStringSpan (byte=, codepoint=, grapheme=)StringSpan (byte=, codepoint=, grapheme=)N/A

Use the keywords for String-related processing. For example:

var s = "The café is a toasty 20°C."
var string_span: StringSpan = s[codepoint=4:8] # café

Cautions:

  • Strings can't be indexed or sliced directly. Always use a keyword to name the unit: byte=, codepoint=, or grapheme=.
  • Strided slices produce copies, not views. Even a step of 1 copies. lst[2:4] is a Span and lst[2:4:1] is a new List.
  • Indexes can't be negative: lst[-1] is an error. Use lst[len(lst) - 1] to reach the last element.
  • Negative bounds abort in a contiguous slice but count from the end in a strided one. lst[-3:] aborts while lst[-3::1] returns the last three elements.
  • A slice whose start exceeds its end must have a step. A positive step results in an empty List. A negative step walks backward.
  • Never use a zero step. The slice returns an empty List instead of raising an error.
  • Types outside the standard collections can define their own subscript rules.

Slices are Mojo built-ins, so you don't need to import them.

Structs​

Functions​

  • ​slice: Constructs a slice from the beginning up to end.