...
This commit is contained in:
7
rhai_engine/rhaibook/start/examples/index.md
Normal file
7
rhai_engine/rhaibook/start/examples/index.md
Normal file
@@ -0,0 +1,7 @@
|
||||
Examples
|
||||
========
|
||||
|
||||
{{#include ../../links.md}}
|
||||
|
||||
Rhai comes with a number of examples showing how to integrate the scripting [`Engine`] within a Rust
|
||||
application, as well as a number of sample scripts that showcase different Rhai language features.
|
73
rhai_engine/rhaibook/start/examples/rust.md
Normal file
73
rhai_engine/rhaibook/start/examples/rust.md
Normal file
@@ -0,0 +1,73 @@
|
||||
Rust Examples
|
||||
=============
|
||||
|
||||
{{#include ../../links.md}}
|
||||
|
||||
|
||||
Standard Examples
|
||||
-----------------
|
||||
|
||||
A number of examples can be found under `examples`.
|
||||
|
||||
| Example | Description |
|
||||
| ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`arrays_and_structs`]({{repoHome}}/examples/arrays_and_structs.rs) | shows how to register a [Rust type][custom type] and using it with [arrays] |
|
||||
| [`callback`]({{repoHome}}/examples/callback.rs) | shows how to store a Rhai [closure] and call it later within Rust |
|
||||
| [`custom_types_and_methods`]({{repoHome}}/examples/custom_types_and_methods.rs) | shows how to register a [Rust type][custom type] and [methods]/[getters/setters] for it |
|
||||
| [`custom_types`]({{repoHome}}/examples/custom_types.rs) | shows how to register a [Rust type][custom type] and [methods]/[getters/setters] using the [`CustomType`] trait. |
|
||||
| [`definitions`]({{repoHome}}/examples/definitions) | shows how to generate definition files for use with the [Rhai Language Server][lsp] (requires the [`metadata`] feature) |
|
||||
| [`hello`]({{repoHome}}/examples/hello.rs) | simple example that evaluates an expression and prints the result |
|
||||
| [`pause_and_resume`]({{repoHome}}/pause_and_resume.rs) | shows how to pause/resume/stop an `Engine` running in a separate thread via an MPSC channel |
|
||||
| [`reuse_scope`]({{repoHome}}/examples/reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common [`Scope`] |
|
||||
| [`serde`]({{repoHome}}/examples/serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the [`serde`] feature) |
|
||||
| [`simple_fn`]({{repoHome}}/examples/simple_fn.rs) | shows how to register a simple Rust function |
|
||||
| [`strings`]({{repoHome}}/examples/strings.rs) | shows different ways to register Rust functions taking [string] arguments |
|
||||
| [`threading`]({{repoHome}}/examples/threading.rs) | shows how to communicate in duplex with an [`Engine`] running in a separate thread via a pair of MPSC channels |
|
||||
|
||||
|
||||
Scriptable Event Handler With State Examples
|
||||
--------------------------------------------
|
||||
|
||||
Because of its popularity, the pattern [_Scriptable Event Handler With State_]({{rootUrl}}/patterns/events.md)
|
||||
has sample implementations for different styles.
|
||||
|
||||
| Example | Handler Script | Description |
|
||||
| ---------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | :----------------------------------------------: |
|
||||
| [`event_handler_main`]({{repoHome}}/examples/event_handler_main) | [`event_handler_main/script.rhai`]({{repoHome}}/examples/event_handler_main/script.rhai) | [_Main Style_]({{rootUrl}}/patterns/events-1.md) |
|
||||
| [`event_handler_js`]({{repoHome}}/examples/event_handler_js) | [`event_handler_js/script.rhai`]({{repoHome}}/examples/event_handler_js/script.rhai) | [_JS Style_]({{rootUrl}}/patterns/events-2.md) |
|
||||
| [`event_handler_map`]({{repoHome}}/examples/event_handler_map) | [`event_handler_map/script.rhai`]({{repoHome}}/examples/event_handler_map/script.rhai) | [_Map Style_]({{rootUrl}}/patterns/events-3.md) |
|
||||
|
||||
|
||||
Running Examples
|
||||
----------------
|
||||
|
||||
Examples can be run with the following command:
|
||||
|
||||
```sh
|
||||
cargo run --example {example_name}
|
||||
```
|
||||
|
||||
`no-std` Examples
|
||||
-----------------
|
||||
|
||||
To illustrate `no-std` builds, a number of example applications are available under the `no_std` directory:
|
||||
|
||||
| Example | Description | Optimization | Allocator | Panics |
|
||||
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------- | :----------: | :-----------------------------------------------: | :----: |
|
||||
| [`no_std_test`]({{repoHome}}/no_std/no_std_test) | bare-bones test application that evaluates a Rhai expression and sets the result as the return value | size | [`wee_alloc`](https://crates.io/crates/wee_alloc) | abort |
|
||||
|
||||
|
||||
### Building the `no-std` examples
|
||||
|
||||
```admonish warning "Nightly required"
|
||||
|
||||
Currently, the nightly compiler must be used to build for `no-std`.
|
||||
```
|
||||
|
||||
```sh
|
||||
cd no_std/no_std_test
|
||||
|
||||
cargo +nightly build --release
|
||||
|
||||
./target/release/no_std_test
|
||||
```
|
59
rhai_engine/rhaibook/start/examples/scripts.md
Normal file
59
rhai_engine/rhaibook/start/examples/scripts.md
Normal file
@@ -0,0 +1,59 @@
|
||||
Example Scripts
|
||||
===============
|
||||
|
||||
{{#include ../../links.md}}
|
||||
|
||||
Language Feature Scripts
|
||||
------------------------
|
||||
|
||||
There are also a number of examples scripts that showcase Rhai's features, all in the `scripts` directory:
|
||||
|
||||
| Script | Description |
|
||||
| ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
||||
| [`array.rhai`]({{repoHome}}/scripts/array.rhai) | [arrays] example |
|
||||
| [`assignment.rhai`]({{repoHome}}/scripts/assignment.rhai) | [variable] declarations |
|
||||
| [`comments.rhai`]({{repoHome}}/scripts/comments.rhai) | just regular [comments] |
|
||||
| [`doc-comments.rhai`]({{repoHome}}/scripts/doc-comments.rhai) | [doc-comments] example |
|
||||
| [`for1.rhai`]({{repoHome}}/scripts/for1.rhai) | [`for`] loops |
|
||||
| [`for2.rhai`]({{repoHome}}/scripts/for2.rhai) | [`for`] loops with [array] iterations |
|
||||
| [`for3.rhai`]({{repoHome}}/scripts/for3.rhai) | [`for`] loops with [closures] |
|
||||
| [`function_decl1.rhai`]({{repoHome}}/scripts/function_decl1.rhai) | a [function] without parameters |
|
||||
| [`function_decl2.rhai`]({{repoHome}}/scripts/function_decl2.rhai) | a [function] with two parameters |
|
||||
| [`function_decl3.rhai`]({{repoHome}}/scripts/function_decl3.rhai) | a [function] with many parameters |
|
||||
| [`function_decl4.rhai`]({{repoHome}}/scripts/function_decl4.rhai) | a [function] acting as a [method]({{rootUrl}}/language/fn-method.md) |
|
||||
| [`function_decl5.rhai`]({{repoHome}}/scripts/function_decl5.rhai) | multiple [functions] as [methods]({{rootUrl}}/language/fn-method.md) for different data types |
|
||||
| [`if1.rhai`]({{repoHome}}/scripts/if1.rhai) | [`if`] example |
|
||||
| [`if2.rhai`]({{repoHome}}/scripts/if2.rhai) | [`if`]-expression example |
|
||||
| [`loop.rhai`]({{repoHome}}/scripts/loop.rhai) | count-down [`loop`] in Rhai, emulating a [`do`] ... `while` loop |
|
||||
| [`module.rhai`]({{repoHome}}/scripts/module.rhai) | import a script file as a module |
|
||||
| [`oop.rhai`]({{repoHome}}/scripts/oop.rhai) | simulate [object-oriented programming (OOP)][OOP] with [closures] |
|
||||
| [`op1.rhai`]({{repoHome}}/scripts/op1.rhai) | just simple addition |
|
||||
| [`op2.rhai`]({{repoHome}}/scripts/op2.rhai) | simple addition and multiplication |
|
||||
| [`op3.rhai`]({{repoHome}}/scripts/op3.rhai) | change evaluation order with parenthesis |
|
||||
| [`string.rhai`]({{repoHome}}/scripts/string.rhai) | [string] operations, including _interpolation_ |
|
||||
| [`strings_map.rhai`]({{repoHome}}/scripts/strings_map.rhai) | [string] and [object map] operations |
|
||||
| [`switch.rhai`]({{repoHome}}/scripts/switch.rhai) | [`switch`] example |
|
||||
| [`while.rhai`]({{repoHome}}/scripts/while.rhai) | [`while`] loop |
|
||||
|
||||
|
||||
Benchmark Scripts
|
||||
-----------------
|
||||
|
||||
The following scripts are for benchmarking the speed of Rhai:
|
||||
|
||||
| Scripts | Description |
|
||||
| --------------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
||||
| [`speed_test.rhai`]({{repoHome}}/scripts/speed_test.rhai) | a simple application to measure the speed of Rhai's interpreter (1 million iterations) |
|
||||
| [`primes.rhai`]({{repoHome}}/scripts/primes.rhai) | use Sieve of Eratosthenes to find all primes smaller than a limit |
|
||||
| [`fibonacci.rhai`]({{repoHome}}/scripts/fibonacci.rhai) | calculate the n-th Fibonacci number using a really dumb algorithm |
|
||||
| [`mat_mul.rhai`]({{repoHome}}/scripts/mat_mul.rhai) | matrix multiplication test to measure the speed of multi-dimensional array access |
|
||||
|
||||
|
||||
Run Example Scripts
|
||||
-------------------
|
||||
|
||||
The [`rhai-run`]({{rootUrl}}/bin.md) utility can be used to run Rhai scripts:
|
||||
|
||||
```sh
|
||||
cargo run --bin rhai-run scripts/any_script.rhai
|
||||
```
|
Reference in New Issue
Block a user