Create a Custom Package as an Independent Crate

Creating a custom package as an independent crate allows it to be shared by multiple projects.

Key concepts

  • Create a Rust crate that specifies rhai as dependency.

  • The main module can contain the package being constructed.


The projects rhai-rand and rhai-sci show simple examples of creating a custom package as an independent crate.



name = "my-package"     # 'my-package' crate

rhai = "1.18.0"    # assuming 1.18.0 is the latest version

use rhai::def_package;
use rhai::plugin::*;

// This is a plugin module
mod my_module {
    // Constants are ignored when used as a package
    pub const MY_NUMBER: i64 = 42;

    pub fn greet(name: &str) -> String {
        format!("hello, {}!", name)
    pub fn get_num() -> i64 {

    // This is a sub-module, but if using combine_with_exported_module!, it will
    // be flattened and all functions registered at the top level.
    pub mod my_sub_module {
        pub fn get_sub_num() -> i64 {

// Define the package 'MyPackage' which is exported for the crate.
def_package! {
    /// My own personal super package in a new crate!
    pub MyPackage(module) {
        combine_with_exported_module!(module, "my-functions", my_module);