Skip to main content

Mojo struct

InlineString

A string that performs small-string optimization to avoid heap allocations for short strings.

Aliases

  • SMALL_CAP = 24: The number of bytes of string data that can be stored inline in this string before a heap allocation is required.
  • Layout = Variant[String, _FixedString[24]]:

Implemented traits

AnyType, CollectionElement, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable, Sized, Stringable

Methods

__init__

__init__(out self)

Constructs a new empty string.

__init__(out self, literal: StringLiteral)

Constructs a InlineString value given a string literal.

Args:

  • literal (StringLiteral): The input constant string.

__init__(out self, owned heap_string: String)

Construct a new small string by taking ownership of an existing heap-allocated String.

Args:

  • heap_string (String): The heap string to take ownership of.

__init__(out self, *, other: Self)

Copy the object.

Args:

  • other (Self): The value to copy.

__add__

__add__(self, other: StringLiteral) -> Self

Construct a string by appending another string at the end of this string.

Args:

  • other (StringLiteral): The string to append.

Returns:

A new string containing the concatenation of self and other.

__add__(self, other: String) -> Self

Construct a string by appending another string at the end of this string.

Args:

  • other (String): The string to append.

Returns:

A new string containing the concatenation of self and other.

__add__(self, other: Self) -> Self

Construct a string by appending another string at the end of this string.

Args:

  • other (Self): The string to append.

Returns:

A new string containing the concatenation of self and other.

__iadd__

__iadd__(inout self, literal: StringLiteral)

Appends another string to this string.

Args:

  • literal (StringLiteral): The string to append.

__iadd__(inout self, string: String)

Appends another string to this string.

Args:

  • string (String): The string to append.

__iadd__(inout self, str_slice: StringSlice[origin])

Appends another string to this string.

Args:

  • str_slice (StringSlice[origin]): The string to append.

__len__

__len__(self) -> Int

Gets the string length, in bytes.

Returns:

The string length, in bytes.

__str__

__str__(self) -> String

Gets this string as a standard String.

Returns:

The string representation of the type.

unsafe_ptr

unsafe_ptr(self) -> UnsafePointer[SIMD[uint8, 1]]

Returns a pointer to the bytes of string data.

Returns:

The pointer to the underlying memory.

as_string_slice

as_string_slice(ref self) -> StringSlice[$1]

Returns a string slice of the data owned by this inline string.

Returns:

A string slice pointing to the data owned by this inline string.

as_bytes

as_bytes(ref self) -> Span[SIMD[uint8, 1], $1]

Returns a contiguous slice of the bytes owned by this string.

This does not include the trailing null terminator.

Returns:

A contiguous slice pointing to the bytes owned by this string.