Skip to main content

Mojo trait

Writer

A destination for formatted text output.

Writer is implemented by types that can accept UTF-8 formatted text, such as strings, files, or network sockets. Types implementing Writable write their output to a Writer.

The core method is write_string(), which accepts a StringSlice for efficient, allocation-free output. The convenience method write() accepts multiple Writable arguments.

Example:

struct StringBuilder(Writer):
    var s: String

    def write_string(mut self, string: StringSlice):
        self.s += string

var builder = StringBuilder("")
builder.write("Count: ", 42)  # Writes multiple values at once
print(builder.s)  # Count: 42

Implemented traits

AnyType, ImplicitlyDestructible

Required methods

write_string

write_string(mut self: _Self, string: StringSlice[string.origin])

Write a StringSlice to this Writer.

Args:

  • string (StringSlice): The string slice to write to this Writer.

Provided methods

write

write[*Ts: Writable](mut self: _Self, *args: *Ts)

Write a sequence of Writable arguments to the provided Writer.

Parameters:

  • *Ts (Writable): Types of the provided argument sequence.

Args:

  • *args (*Ts): Sequence of arguments to write to this Writer.

Was this page helpful?