Rhai is an [[MIT License]]/[[Apache v2]] dual licensed embeddable scripting language written in [[Rust]]. - [Website](https://rhai.rs/) - [Source](https://github.com/rhaiscript/rhai) - [Documentation](https://rhai.rs/book/) (book) - [Documentation](https://docs.rs/rhai/1) (Rust API) - [Crate](https://crates.io/crates/rhai) > A small, fast, easy-to-use scripting language and evaluation engine that integrates tightly with Rust. Builds for most common targets including no-std and WASM. # Notability Despite it's weirdly limited feature set it seems relatively popular. Reminds me of early rigid video game scripting languages like [[AGI Script]], Doom's [[ASC Script]], or [[Earthbound Control Codes]] that expects most things to be written and registered in the host language. At this point I don't personally see a use for it. # Philosophy > 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. # Platform Support - All CPU and O/S targets supported by Rust, including: - WebAssembly (WASM) - `no-std` - Minimum Rust version 1.66.0 # Syntax ```rust //! This script defines multiple versions of the same function //! for use as method with different data types. // For strings fn string.calc(x) { this.len + x } // For integers fn int.calc(x) { this * x } // For booleans fn bool.calc(x) { if this { x } else { 0} } // For arrays fn array.calc(x) { this.len + x } // For object maps fn map.calc(x) { this[x] } // Catch-all fn calc(x) { `${this}: ${x}` } print("hello".calc(42)); // 47 print(42.calc(42)); // 1764 print(true.calc(42)); // 42 print(false.calc(42)); // 0 print([1,2,3].calc(42)); // 45 print(#{"a": 1, "b": 2}.calc("b")); // 2 print('x'.calc(42)); // x: 42 ``` # Features ## Anti-Features - **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_](https://rhai.rs/book/rust/custom-types.html) that implements `Clone`. - **No first-class functions** – Code your functions in Rust instead, and register them with Rhai. - **No garbage collection** – this should be expected, so… - **No first-class closures** – do your closure magic in Rust instead: [turn a Rhai scripted function into a Rust closure](https://rhai.rs/book/engine/call-fn.html). - **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. - **No bytecodes/JIT** – Rhai uses a heavily-optimized AST-walking interpreter which is fast enough for most real-life scenarios. # Tips # References