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, ")")
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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
If you'd like to share more information, please report an issue on GitHub
π What went wrong?