Values and Types

The following primitive types are supported natively:

CategoryEquivalent Rust typestype_of()to_string()
Integer numberu8, i8, u16, i16,
u32, i32 (default for only_i32),
u64, i64 (default)
"i32", "u64" etc."42", "123" etc.
Floating-point number (disabled with no_float)f32 (default for f32_float), f64 (default)"f32" or "f64""123.4567" etc.
Fixed precision decimal number (requires decimal)Decimal"decimal""42", "123.4567" etc.
Boolean valuebool"bool""true" or "false"
Unicode characterchar"char""A", "x" etc.
Immutable Unicode stringrhai::ImmutableString (implemented as Rc<SmartString> or Arc<SmartString>)"string""hello" etc.
Array (disabled with no_index)rhai::Array"array""[ 1, 2, 3 ]"
Object map (disabled with no_object)rhai::Map"map""#{ "a": 1, "b": 2 }"
Timestamp (disabled with no_std)std::time::Instant (instant::Instant if WASM build)"timestamp""<timestamp>"
Function pointerrhai::FnPtrFn"Fn(foo)"
Dynamic value (i.e. can be anything)rhai::Dynamicthe actual typeactual value
Shared value (a reference-counted, shared Dynamic value, created via automatic currying, disabled with no_closure)the actual typeactual value
System integer (current configuration)rhai::INT (i32 or i64)"i32" or "i64""42", "123" etc.
System floating-point (current configuration, disabled with no_float)rhai::FLOAT (f32 or f64)"f32" or "f64""123.456" etc.
Nothing/void/nil/null/Unit (or whatever it is called)()"()""" (empty string)

All types are treated strictly separate by Rhai, meaning that i32 and i64 and u32 are completely different - they even cannot be added together. This is very similar to Rust.

The default integer type is i64. If other integer types are not needed, it is possible to exclude them and make a smaller build with the only_i64 feature.

If only 32-bit integers are needed, enabling the only_i32 feature will remove support for all integer types other than i32, including i64. This is useful on some 32-bit targets where using 64-bit integers incur a performance penalty.

If no floating-point is needed or supported, use the no_float feature to remove it.

Some applications require fixed-precision decimal numbers, which can be enabled via the decimal feature.

Strings in Rhai are immutable, meaning that they can be shared but not modified. Internally, the ImmutableString type is a wrapper over Rc<String> or Arc<String> (depending on sync). Any modification done to a Rhai string causes the string to be cloned and the modifications made to the copy.

The to_string() function converts a standard type into a string for display purposes.

The to_debug() function converts a standard type into a string in debug format.