Printing for Custom Types
To use custom types for print
and debug
, or convert its value into a string,
it is necessary that the following functions be registered (assuming the custom type
is T: Display + Debug
):
Function | Signature | Typical implementation | Usage |
---|---|---|---|
to_string | |x: &mut T| -> String | x.to_string() | converts the custom type into a string |
print | |x: &mut T| -> String | x.to_string() | converts the custom type into a string for the print statement |
to_debug | |x: &mut T| -> String | format!("{:?}", x) | converts the custom type into a string in debug format |
debug | |x: &mut T| -> String | format!("{:?}", x) | converts the custom type into a string for the debug statement |
+ | |s: &str, x: T| -> String | format!("{}{}", s, x) | concatenates the custom type with another string |
+ | |x: &mut T, s: &str| -> String | x.to_string().push_str(s); | concatenates another string with the custom type |
+= | |s: &mut ImmutableString, x: T| | s += x.to_string() | appends the custom type to an existing string |