Skip to main content
Log in

Mojo 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))))
var msg: String = "Hello Mojo"

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

Fields​

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

Implemented traits​

AnyType, CollectionElementNew, Copyable, EqualityComparable, ExplicitlyCopyable, Formattable, Movable, Representable, 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__(inout self: Self, start: Optional[Int], end: Optional[Int], step: Optional[Int])

Construct slice given the start, end and step values.

Args:

  • ​start (Optional[Int]): The start value.
  • ​end (Optional[Int]): The end value.
  • ​step (Optional[Int]): The step value.

__init__(inout self: Self, *, other: Self)

Creates a deep copy of the Slice.

Args:

  • ​other (Self): The slice to copy.

__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.

__str__​

__str__(self: Self) -> String

Gets the string representation of the span.

Returns:

The string representation of the span.

__repr__​

__repr__(self: Self) -> String

Gets the string representation of the span.

Returns:

The string representation of the span.

format_to​

format_to(self: Self, inout writer: Formatter)

Write Slice string representation to a Formatter.

Args:

  • ​writer (Formatter): The formatter to write to.

indices​

indices(self: Self, length: Int) -> Tuple[Int, Int, Int]

Returns a tuple of 3 integers representing the start, end, and step of the slice if applied to a container of the given length.

Uses the target container length to normalize negative, out of bounds, or None indices.

Negative indices are wrapped using the length of the container.

s = slice(0, -1, 1)
s.indices(5) # returns (0, 4, 1)
s = slice(0, -1, 1)
s.indices(5) # returns (0, 4, 1)

None indices are defaulted to the start or the end of the container based on whether step is positive or negative.

s = slice(None, None, 1)
s.indices(5) # returns (0, 5, 1)
s = slice(None, None, 1)
s.indices(5) # returns (0, 5, 1)

Out of bounds indices are clamped using the size of the container.

s = slice(20)
s.indices(5) # returns (0, 5, 1)
s = slice(20)
s.indices(5) # returns (0, 5, 1)

Args:

  • ​length (Int): The length of the target container.

Returns:

A tuple containing three integers for start, end, and step.

Was this page helpful?