profile picture

Rhai 0.2 release

March 26, 2016 - rust rhai

I've just updated Rhai to 0.2. This release focuses on improving the interaction between Rhai and Rust. The result is a cleaner, easier-to-use API. This new API does mean some API breakages, so you'll need to update your code to work with 0.2.

With this release, Rhai also now has arrays as a built-in type.

You can upgrade/install Rhai from cargo.

API improvements

Rhai 0.2 has a lot of small improvements that give it a much more natural feel in Rust. The main three areas are function registration, getters/setter helpers, and simpler eval.

Function registration

Function registration now works through method calls on the engine, and we can infer the expected function type. The end result is generally much cleaner.


&(add as fn(x: i32, y: i32)->i32).register(&mut engine, "add");


engine.register_fn("add", add);

Getter/setter helpers

To help cut down on repetition, Rhai 0.2 has a few helpers for working with getters and setters.


&(TestStruct::get_x as fn(&mut TestStruct)->i32).register(&mut engine, "get$x");
&(TestStruct::set_x as fn(&mut TestStruct, i32)->()).register(&mut engine, "set$x");


engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);

Simpler eval

Eval will now take the expected return type as a type parameter, letting you cut out a few steps. We've also moved to using &str rather than String for eval inputs.


if let Ok(result) = engine.eval("40 + 2".to_string()).unwrap().downcast::<i32>() {}


if let Ok(result) = engine.eval::<i32>("40 + 2") {}

New feature: arrays

In addition to the API improvements, Rhai now has a built-in array type.

var y = [1, 2, 3]; 
y[1] = 5; 



Thanks for the feedback on Rhai's first release. Also, big thanks to @nikomatsakis, who gave me a few key pointers on how improve the API.

Looking forward to hearing from you.