update README

This commit is contained in:
Lukáš Hozda 2017-11-04 11:18:56 +01:00
parent 8fbeecb31b
commit df6ac00a67

View File

@ -12,7 +12,7 @@ Rhai's current feature set:
* No additional dependencies * No additional dependencies
* No unsafe code * No unsafe code
**Note:** Currently, it's version 0.7.0, so the language and APIs may change before they stabilize.* **Note:** Currently, the version is 0.7.1, so the language and APIs may change before they stabilize.*
## Installation ## Installation
@ -20,7 +20,7 @@ You can install Rhai using crates by adding this line to your dependences:
``` ```
[dependencies] [dependencies]
rhai = "0.7.0" rhai = "0.7.1"
``` ```
## Related ## Related
@ -71,7 +71,7 @@ cargo run --example rhai_runner scripts/any_script.rhai
To get going with Rhai, you create an instance of the scripting engine and then run eval. To get going with Rhai, you create an instance of the scripting engine and then run eval.
```Rust ```rust
extern crate rhai; extern crate rhai;
use rhai::Engine; use rhai::Engine;
@ -86,7 +86,7 @@ fn main() {
You can also evaluate a script file: You can also evaluate a script file:
```Rust ```rust
if let Ok(result) = engine.eval_file::<i64>("hello_world.rhai") { ... } if let Ok(result) = engine.eval_file::<i64>("hello_world.rhai") { ... }
``` ```
@ -94,7 +94,7 @@ if let Ok(result) = engine.eval_file::<i64>("hello_world.rhai") { ... }
Rhai's scripting engine is very lightweight. It gets its ability from the functions in your program. To call these functions, you need to register them with the scripting engine. Rhai's scripting engine is very lightweight. It gets its ability from the functions in your program. To call these functions, you need to register them with the scripting engine.
```Rust ```rust
extern crate rhai; extern crate rhai;
use rhai::{Engine, FnRegister}; use rhai::{Engine, FnRegister};
@ -117,7 +117,7 @@ fn main() {
Generic functions can be used in Rhai, but you'll need to register separate instances for each concrete type: Generic functions can be used in Rhai, but you'll need to register separate instances for each concrete type:
```Rust ```rust
use std::fmt::Display; use std::fmt::Display;
extern crate rhai; extern crate rhai;
@ -142,7 +142,7 @@ You can also see in this example how you can register multiple functions (or in
Here's an more complete example of working with Rust. First the example, then we'll break it into parts: Here's an more complete example of working with Rust. First the example, then we'll break it into parts:
```Rust ```rust
extern crate rhai; extern crate rhai;
use rhai::{Engine, FnRegister}; use rhai::{Engine, FnRegister};
@ -177,7 +177,7 @@ fn main() {
First, for each type we use with the engine, we need to be able to Clone. This allows the engine to pass by value and still keep its own state. First, for each type we use with the engine, we need to be able to Clone. This allows the engine to pass by value and still keep its own state.
```Rust ```rust
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: i64 x: i64
@ -185,7 +185,7 @@ struct TestStruct {
``` ```
Next, we create a few methods that we'll later use in our scripts. Notice that we register our custom type with the engine. Next, we create a few methods that we'll later use in our scripts. Notice that we register our custom type with the engine.
```Rust ```rust
impl TestStruct { impl TestStruct {
fn update(&mut self) { fn update(&mut self) {
self.x += 1000; self.x += 1000;
@ -205,13 +205,13 @@ To use methods and functions with the engine, we need to register them. There a
*Note: the engine follows the convention that methods use a &mut first parameter so that invoking methods can update the value in memory.* *Note: the engine follows the convention that methods use a &mut first parameter so that invoking methods can update the value in memory.*
```Rust ```rust
engine.register_fn("update", TestStruct::update); engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
``` ```
Finally, we call our script. The script can see the function and method we registered earlier. We need to get the result back out from script land just as before, this time casting to our custom struct type. Finally, we call our script. The script can see the function and method we registered earlier. We need to get the result back out from script land just as before, this time casting to our custom struct type.
```Rust ```rust
if let Ok(result) = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x") { if let Ok(result) = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x") {
println!("result: {}", result.x); // prints 1001 println!("result: {}", result.x); // prints 1001
} }
@ -223,7 +223,7 @@ Similarly, you can work with members of your custom types. This works by regist
For example: For example:
```Rust ```rust
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: i64 x: i64
@ -261,7 +261,7 @@ By default, Rhai treats each engine invocation as a fresh one, persisting only t
In this example, we thread the same state through multiple invocations: In this example, we thread the same state through multiple invocations:
```Rust ```rust
extern crate rhai; extern crate rhai;
use rhai::{Engine, Scope}; use rhai::{Engine, Scope};
@ -281,18 +281,18 @@ fn main() {
## Variables ## Variables
```Rust ```rust
let x = 3; let x = 3;
``` ```
## Operators ## Operators
```Rust ```rust
let x = (1 + 2) * (6 - 4) / 2; let x = (1 + 2) * (6 - 4) / 2;
``` ```
## If ## If
```Rust ```rust
if true { if true {
print("it's true!"); print("it's true!");
} }
@ -302,7 +302,7 @@ else {
``` ```
## While ## While
```Rust ```rust
let x = 10; let x = 10;
while x > 0 { while x > 0 {
print(x); print(x);
@ -328,7 +328,7 @@ loop {
Rhai supports defining functions in script: Rhai supports defining functions in script:
```Rust ```rust
fn add(x, y) { fn add(x, y) {
return x + y; return x + y;
} }
@ -338,7 +338,7 @@ print(add(2, 3))
Just like in Rust, you can also use an implicit return. Just like in Rust, you can also use an implicit return.
```Rust ```rust
fn add(x, y) { fn add(x, y) {
x + y x + y
} }
@ -349,7 +349,7 @@ print(add(2, 3))
You can create arrays of values, and then access them with numeric indices. You can create arrays of values, and then access them with numeric indices.
```Rust ```rust
let y = [1, 2, 3]; let y = [1, 2, 3];
y[1] = 5; y[1] = 5;
@ -358,7 +358,7 @@ print(y[1]);
## Members and methods ## Members and methods
```Rust ```rust
let a = new_ts(); let a = new_ts();
a.x = 500; a.x = 500;
a.update(); a.update();
@ -366,7 +366,7 @@ a.update();
## Strings and Chars ## Strings and Chars
```Rust ```rust
let name = "Bob"; let name = "Bob";
let middle_initial = 'C'; let middle_initial = 'C';
``` ```
@ -386,3 +386,33 @@ let /* intruder comment */ name = "Bob";
/*/*/*/*/**/*/*/*/*/ /*/*/*/*/**/*/*/*/*/
*/ */
``` ```
## Unary operators
```rust
let number = -5;
number = -5 - +5;
let booly = !true;
```
## Compount assignment operators
```rust
let number = 5;
number += 4;
number -= 3;
number *= 2;
number /= 1;
number %= 3;
number <<= 2;
number >>= 1;
```
The `+=` operator can also be used to build strings:
```rust
let my_str = "abc";
my_str += "ABC";
my_str == "abcABC"
```