9. Strings
This chapter covers rendering a value into a string (the type: §4 Strings; its literals: §3 String literals, §3 String interpolation).
Compile-time verification#
Each ${...} is type-checked with the program and renders through its type's Display impl. An unknown name, a malformed expression or a value whose type lacks Display (an enum with neither @displayable nor an impl, a struct without an impl, an array of such a type, a type parameter without a Display bound) is an error.
Display trait#
Every primitive and null (rendered "null") implement Display.
An array renders its elements through their own Display, separated by , between brackets ("${[[1], [2, 3]]}" is "[[1], [2, 3]]"), but does not satisfy a T: Display bound: show(xs) is an error for show :: fn<T: Display>(x: T).
An enum gets Display from @displayable (§4 Enums), any other user type from an impl:
Point implements Display {
to_string :: fn(self) -> string {
return "(${self.x}, ${self.y})"
}
}