rhai/doc/src/rust/print-custom.md
2020-10-12 23:17:22 +08:00

1.6 KiB

Printing for Custom Types

{{#include ../links.md}}

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
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]