Skip to main content

struct

Slice

Represents a slice expression.

Objects of this type are generated when slice syntax is used within square brackets, e.g.:

var msg: String = "Hello Mojo"

# Both are equivalent and print "Mojo".
print(msg[6:])
print(msg.__getitem__(Slice(6, len(msg))))

Fields

  • start (Int): The starting index of the slice.
  • end (Int): The end index of the slice.
  • step (Int): The step increment value of the slice.

Implemented traits

AnyType, EqualityComparable, Stringable

Methods

__init__

__init__(inout self: Self, start: Int, end: Int)

Construct slice given the start and end values.

Args:

  • start (Int): The start value.
  • end (Int): The end value.

__init__[T0: AnyTrivialRegType, T1: AnyTrivialRegType, T2: AnyTrivialRegType](inout self: Self, start: T0, end: T1, step: T2)

Construct slice given the start, end and step values.

Parameters:

  • T0 (AnyTrivialRegType): Type of the start value.
  • T1 (AnyTrivialRegType): Type of the end value.
  • T2 (AnyTrivialRegType): Type of the step value.

Args:

  • start (T0): The start value.
  • end (T1): The end value.
  • step (T2): The step value.

__getitem__

__getitem__(self: Self, idx: Int) -> Int

Get the slice index.

Args:

  • idx (Int): The index.

Returns:

The slice index.

__eq__

__eq__(self: Self, other: Self) -> Bool

Compare this slice to the other.

Args:

  • other (Self): The slice to compare to.

Returns:

True if start, end, and step values of this slice match the corresponding values of the other slice and False otherwise.

__ne__

__ne__(self: Self, other: Self) -> Bool

Compare this slice to the other.

Args:

  • other (Self): The slice to compare to.

Returns:

False if start, end, and step values of this slice match the corresponding values of the other slice and True otherwise.

unsafe_indices

unsafe_indices(self: Self) -> Int

Return the length of the slice.

Only use this function if start/end is guaranteed to be not None.

Returns:

The length of the slice.