Skip to main content
Log in

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__(inout self: Self)

Constructs a new empty string.

__init__(inout self: Self, literal: StringLiteral)

Constructs a InlineString value given a string literal.

Args:

  • literal (StringLiteral): The input constant string.

__init__(inout self: 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__(inout self: Self, *, other: Self)

Copy the object.

Args:

  • other (Self): The value to copy.

__add__

__add__(self: 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: 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: 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: Self, literal: StringLiteral)

Appends another string to this string.

Args:

  • literal (StringLiteral): The string to append.

__iadd__(inout self: Self, string: String)

Appends another string to this string.

Args:

  • string (String): The string to append.

__iadd__(inout self: Self, str_slice: StringSlice[is_mutable, lifetime])

Appends another string to this string.

Args:

  • str_slice (StringSlice[is_mutable, lifetime]): The string to append.

__len__

__len__(self: Self) -> Int

Gets the string length, in bytes.

Returns:

The string length, in bytes.

__str__

__str__(self: Self) -> String

Gets this string as a standard String.

Returns:

The string representation of the type.

unsafe_ptr

unsafe_ptr(self: Self) -> UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 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_is_lifetime] self: Self) -> StringSlice[$0, $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_slice

as_bytes_slice(ref [self_is_lifetime] self: Self) -> Span[$0, 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.

Was this page helpful?