Printing for Custom Types

Provide These Functions

To use custom types for print and debug, or convert a custom type into a string, it is necessary that the following functions, at minimum, be registered (assuming the custom type is T: Display + Debug).

FunctionSignatureTypical implementationUsage
to_string|x: &mut T| -> Stringx.to_string()converts the custom type into a string
to_debug|x: &mut T| -> Stringformat!("{:?}", x)converts the custom type into a string in debug format

Tip: #[rhai_fn(global)]

If these functions are defined via a plugin module, be sure to include the #[rhai_fn(global)] attribute in order to make them available globally.

See this section for more details.

Also Consider These

The following functions are implemented using to_string() or to_debug() by default, but can be overloaded with custom versions.

print|x: &mut T| -> Stringto_stringconverts the custom type into a string for the print statement
debug|x: &mut T| -> Stringto_debugconverts the custom type into a string for the debug statement
+ operator|s: &str, x: T| -> Stringto_stringconcatenates the custom type with another string
+ operator|x: &mut T, s: &str| -> Stringto_stringconcatenates another string with the custom type
+= operator|s: &mut ImmutableString, x: T|to_stringappends the custom type to an existing string