Skip to main content

Mojo trait

Formattable

The Formattable trait describes a type that can be converted to a stream of UTF-8 encoded data by writing to a formatter object.

Examples:

Implement Formattable and Stringable for a type:

struct Point(Stringable, Formattable):
var x: Float64
var y: Float64

fn __str__(self) -> String:
return String.format_sequence(self)

fn format_to(self, inout writer: Formatter):
writer.write("(", self.x, ", ", self.y, ")")
struct Point(Stringable, Formattable):
var x: Float64
var y: Float64

fn __str__(self) -> String:
return String.format_sequence(self)

fn format_to(self, inout writer: Formatter):
writer.write("(", self.x, ", ", self.y, ")")

Implemented traits

AnyType

Methods

format_to

format_to(self: T, inout writer: Formatter)

Formats the string representation of this type to the provided formatter.

Args:

  • writer (Formatter): The formatter to write to.