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

    fn 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

comptime members

__del__is_trivial

comptime __del__is_trivial

A flag (often compiler generated) to indicate whether the implementation of __del__ is trivial.

The implementation of __del__ is considered to be trivial if:

  • The struct has a compiler-generated trivial destructor and all its fields have a trivial __del__ method.

In practice, it means that the __del__ can be considered as no-op.

Required methods

write_string

write_string(mut self: _Self, string: StringSlice[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?