Dust is a programming language inspired by Rust and targeted to run on browsers and on JavaScript engines. The runtime works by translating Dust code to JavaScript. The following examples demonstrate the language basics.

Dust → JavaScript

Hello World! let msg = "hello world!"; //console::log(msg); alert(msg);

The keyword let is used to declare a variable. The type of variable is inferred. In the example, using msg + 1 will result in a compilation error because we are trying to add a number to a string. Use // to comment a line.

If statement if 3 > 5 { alert("interesting"); } else { alert("boring"); }

if ... else if ... else blocks need curly braces around the blocks. The brackets around the condition are not required. if ... else can also act as expression. Eg, let max = if a > b { a } else { b };

Match let count = 5; let msg = match count { 0 => "nothing", 1 => "one item", 2 | 3 | 4 => "few items", _ => "many items" }; alert(msg);

Instead of switch , there's match control structure which works by matching a value with a pattern and executing the matching arm. _ has the semantics of otherwise. Just like if , it can have an expression on the RHS or a block of code. It should be noted that the type checking is in place which means that all the RHS must have the same type.

Loop let mut result = ""; let mut i = 0; while i < 3 { i += 1; result += "la"; } alert(result);

Again, brackets around the condition are skipped. The semantics of mut are to indicate if a variable is mutable. However, it is not yet enforced in the language. Operator ++ is not supported, use += 1 .

Function fn double(x) { console::log("doubling..."); x * 2 } alert(double(4));

The last expression is returned. Note that the function defined in the example is not polymorphic. Calling double on a string type will fail. The correct signature for a polymorphic double function is fn double<T>(x: T) -> T

Implementation of KMP algorithm /// returns minimum index where W is found in S fn kmp(S, W) { // build table T let mut pos = 2; let mut cnd = 0; let mut T = []; let mut j = 0; while j < W.length { T.push(0); j += 1; } T.set(0, -1); T.set(1, 0); while pos < W.length { if W.charCodeAt(pos - 1) == W.charCodeAt(cnd) { cnd += 1; T.set(pos, cnd); pos += 1; } else if cnd > 0 { cnd = T.get(cnd); } else { T.set(pos, 0); pos += 1; } } // start the search let mut m = 0; let mut i = 0; while m + i < S.length { if W.charCodeAt(i) == S.charCodeAt(m + i) { if i == (W.length - 1) { return m; } i += 1; } else { if T.get(i) > -1 { i = T.get(i); m += i - T.get(i); } else { i = 0; m += 1; } } } S.length } alert(kmp("ABC ABCDAB ABCDABCDABDE", "ABCDABD"));