Revise using string interpolation.
This commit is contained in:
parent
bc9c1ab850
commit
0807c474a1
@ -1,7 +1,7 @@
|
||||
Rhai Release Notes
|
||||
==================
|
||||
|
||||
This version adds string interpolation.
|
||||
This version adds string interpolation with `` `... ${`` ... ``} ...` `` syntax.
|
||||
|
||||
Version 0.19.16
|
||||
===============
|
||||
|
@ -93,8 +93,8 @@ fn bench_parse_primes(bench: &mut Bencher) {
|
||||
}
|
||||
}
|
||||
|
||||
print("Total " + total_primes_found + " primes.");
|
||||
print("Run time = " + now.elapsed + " seconds.");
|
||||
print(`Total ${total_primes_found} primes.`);
|
||||
print(`Run time = ${now.elapsed} seconds.`);
|
||||
"#;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
@ -40,9 +40,9 @@ fn main() {
|
||||
|
||||
loop {
|
||||
let x = get();
|
||||
print("Script Read: " + x);
|
||||
print(`Script Read: ${x}`);
|
||||
x += 1;
|
||||
print("Script Write: " + x);
|
||||
print(`Script Write: ${x}`);
|
||||
put(x);
|
||||
}
|
||||
"#,
|
||||
|
@ -21,9 +21,9 @@ for n in range(0, 5) {
|
||||
result = fib(target);
|
||||
}
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
||||
print("Fibonacci number #" + target + " = " + result);
|
||||
print(`Fibonacci number #${target} = ${result}`);
|
||||
|
||||
if result != 317_811 {
|
||||
print("The answer is WRONG! Should be 317,811!");
|
||||
|
@ -4,7 +4,7 @@ let arr = [1, 2, 3, 4];
|
||||
|
||||
for a in arr {
|
||||
for b in [10, 20] {
|
||||
print(a + "," + b);
|
||||
print(`${a}, ${b}`);
|
||||
}
|
||||
|
||||
if a == 3 { break; }
|
||||
|
@ -1,6 +1,6 @@
|
||||
const MAX = 1_000_000;
|
||||
|
||||
print("Iterating an array with " + MAX + " items...");
|
||||
print(`Iterating an array with ${MAX} items...`);
|
||||
|
||||
print("Ready... Go!");
|
||||
|
||||
@ -18,5 +18,5 @@ for i in list {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
print("Sum = " + sum);
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Sum = ${sum}`);
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@ -68,4 +68,4 @@ for i in range(0, SIZE) {
|
||||
}
|
||||
*/
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@ -7,7 +7,7 @@ let last_value = ();
|
||||
let obj1 = #{
|
||||
_data: 42, // data field
|
||||
get_data: || this._data, // property getter
|
||||
action: || print("Data=" + this._data), // method
|
||||
action: || print(`Data=${this._data}`), // method
|
||||
update: |x| { // property setter
|
||||
this._data = x;
|
||||
last_value = this._data; // capture 'last_value'
|
||||
@ -38,4 +38,4 @@ if obj2.get_data() > 0 { // property access
|
||||
obj2.update(42); // call method
|
||||
}
|
||||
|
||||
print("Should be 84: " + last_value);
|
||||
print(`Should be 84: ${last_value}`);
|
||||
|
@ -25,8 +25,8 @@ for p in range(2, MAX_NUMBER_TO_CHECK) {
|
||||
}
|
||||
}
|
||||
|
||||
print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK);
|
||||
print("Run time = " + now.elapsed + " seconds.");
|
||||
print(`Total ${total_primes_found} primes <= ${MAX_NUMBER_TO_CHECK}`);
|
||||
print(`Run time = ${now.elapsed} seconds.`);
|
||||
|
||||
if total_primes_found != 78_498 {
|
||||
print("The answer is WRONG! Should be 78,498!");
|
||||
|
@ -10,4 +10,4 @@ while x > 0 {
|
||||
x -= 1;
|
||||
}
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@ -11,17 +11,18 @@ print("foo" >= "bar"); // string comparison
|
||||
print("the answer is " + 42); // string building using non-string types
|
||||
|
||||
let s = "\u2764 hello, world! \U0001F603"; // string variable
|
||||
print("length=" + s.len); // should be 17
|
||||
print(`length=${s.len}`); // should be 17
|
||||
|
||||
s[s.len-3] = '?'; // change the string
|
||||
print("Question: " + s); // should print 'Question: hello, world?'
|
||||
print(`Question: ${s}`); // should print 'Question: hello, world?'
|
||||
|
||||
// Line continuation:
|
||||
let s = "This is a long \
|
||||
string constructed using \
|
||||
line continuation";
|
||||
|
||||
print("One string: " + s);
|
||||
// String interpolation
|
||||
print(`One string: ${s}`);
|
||||
|
||||
// Multi-line literal string:
|
||||
let s = `
|
||||
|
@ -75,7 +75,7 @@ let keys = [];
|
||||
for animal in animals {
|
||||
for adjective in adjectives {
|
||||
for adverb in adverbs {
|
||||
keys.push(adverb + " " + adjective + " " + animal)
|
||||
keys.push(`${adverb} ${adjective} ${animal}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -99,5 +99,5 @@ for key in keys {
|
||||
map.remove(key);
|
||||
}
|
||||
|
||||
print("Sum = " + sum);
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Sum = ${sum}`);
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
12
src/ast.rs
12
src/ast.rs
@ -384,7 +384,7 @@ impl AST {
|
||||
/// "#)?;
|
||||
///
|
||||
/// let ast2 = engine.compile(r#"
|
||||
/// fn foo(n) { "hello" + n }
|
||||
/// fn foo(n) { `hello${n}` }
|
||||
/// foo("!")
|
||||
/// "#)?;
|
||||
///
|
||||
@ -395,7 +395,7 @@ impl AST {
|
||||
///
|
||||
/// // 'ast' is essentially:
|
||||
/// //
|
||||
/// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten
|
||||
/// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
|
||||
/// // foo(1) // <- notice this will be "hello1" instead of 43,
|
||||
/// // // but it is no longer the return value
|
||||
/// // foo("!") // returns "hello!"
|
||||
@ -436,7 +436,7 @@ impl AST {
|
||||
/// "#)?;
|
||||
///
|
||||
/// let ast2 = engine.compile(r#"
|
||||
/// fn foo(n) { "hello" + n }
|
||||
/// fn foo(n) { `hello${n}` }
|
||||
/// foo("!")
|
||||
/// "#)?;
|
||||
///
|
||||
@ -447,7 +447,7 @@ impl AST {
|
||||
///
|
||||
/// // 'ast1' is essentially:
|
||||
/// //
|
||||
/// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten
|
||||
/// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
|
||||
/// // foo(1) // <- notice this will be "hello1" instead of 43,
|
||||
/// // // but it is no longer the return value
|
||||
/// // foo("!") // returns "hello!"
|
||||
@ -490,7 +490,7 @@ impl AST {
|
||||
/// "#)?;
|
||||
///
|
||||
/// let ast2 = engine.compile(r#"
|
||||
/// fn foo(n) { "hello" + n }
|
||||
/// fn foo(n) { `hello${n}` }
|
||||
/// fn error() { 0 }
|
||||
/// foo("!")
|
||||
/// "#)?;
|
||||
@ -574,7 +574,7 @@ impl AST {
|
||||
/// "#)?;
|
||||
///
|
||||
/// let ast2 = engine.compile(r#"
|
||||
/// fn foo(n) { "hello" + n }
|
||||
/// fn foo(n) { `hello${n}` }
|
||||
/// fn error() { 0 }
|
||||
/// foo("!")
|
||||
/// "#)?;
|
||||
|
@ -41,7 +41,7 @@ pub trait FuncArgs {
|
||||
///
|
||||
/// let ast = engine.compile(r#"
|
||||
/// fn hello(x, y, z) {
|
||||
/// if x { "hello " + y } else { y + z }
|
||||
/// if x { `hello ${y}` } else { y + z }
|
||||
/// }
|
||||
/// "#)?;
|
||||
///
|
||||
|
@ -81,7 +81,7 @@ fn test_call_fn_args() -> Result<(), Box<EvalAltResult>> {
|
||||
let ast = engine.compile(
|
||||
r#"
|
||||
fn hello(x, y, z) {
|
||||
if x { "hello " + y } else { y + z }
|
||||
if x { `hello ${y}` } else { y + z }
|
||||
}
|
||||
"#,
|
||||
)?;
|
||||
|
@ -315,7 +315,7 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
// Final variable values become constant module variable values
|
||||
foo = calc(foo);
|
||||
hello = "hello, " + foo + " worlds!";
|
||||
hello = `hello, ${foo} worlds!`;
|
||||
|
||||
export
|
||||
x as abc,
|
||||
|
Loading…
Reference in New Issue
Block a user