Skip to main content

Mojo module

string_slice

Implements the StringSlice type and related utilities for efficient string operations.

comptime values

StaticString

comptime StaticString = StringSlice[StaticConstantOrigin]

An immutable static string slice.

This is a type of StringSlice that's immutable and statically allocated. You might use this for situations that could also be done with a String type, but when you want to optimize memory usage with zero heap allocations.

The key difference from StringSlice is that a StaticString is guaranteed to point to data (string literals, constants) that will never be deallocated (a regular StringSlice may point to any string data that might be freed). This makes StaticString safe to store long-term without lifetime concerns.

Although you can reassign a StaticString-typed variable with a new value, you can't modify the underlying data of a StaticString after it's created the way you can with a String, such as using += to append to it.

Because this is still a StringSlice type, you can do all the same things with it, such as format a string:

var format_string = StaticString("{}: {}")
print(format_string.format("bats", 6))     # => bats: 6

Structs

  • CodepointsIter: Iterator over the Codepoints in a string slice, constructed by StringSlice.codepoints().
  • CodepointSliceIter: Iterator for StringSlice over substring slices containing a single Unicode codepoint.
  • StringSlice: A non-owning view into encoded string data.

Functions

  • get_static_string: Form a StaticString from compile-time StringSlice values. This guarantees that the returned string is compile-time constant in static memory. It also guarantees that there is a 'nul' zero byte at the end, which is not included in the returned range.

Was this page helpful?