What Rhai Isn’t

Rhai’s purpose is to provide a dynamic layer over Rust code, in the same spirit of zero cost abstractions.

It doesn’t attempt to be a new language. For example:

  • No classes. Well, Rust doesn’t either. On the other hand…

  • No traits… so it is also not Rust. Do your Rusty stuff in Rust.

  • No structures/records/tuples – define your types in Rust instead; Rhai can seamlessly work with any Rust type that implements Clone.

Object maps

There is a built-in object map type which is adequate for most uses.

It is also possible to simulate object-oriented programming (OOP) by storing function pointers or closures in object map properties, turning them into methods.

  • No first-class functions – Code your functions in Rust instead, and register them with Rhai.

Function pointers

There is support for simple function pointers to allow runtime dispatch by function name.

Simulated closures

There is support for simulated closures via currying a function pointer with captured shared variables.

  • No formal language grammar – Rhai uses a hand-coded lexer, a hand-coded top-down recursive-descent parser for statements, and a hand-coded Pratt parser for expressions.

Highly customizable

This lack of formalism allows the tokenizer and parser themselves to be exposed as services in order to support a wide range of user customizations, such as:

  • No bytecodes/JIT – Rhai uses a heavily-optimized AST-walking interpreter which is fast enough for most real-life scenarios.

How it compares?

See Rhai performance benchmarks.