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.
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
... 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 };
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.
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
.
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
The function kmp
implements the Knuth-Morris-Pratt string searching algorithm and is based on the pseudocode provided on the wikipedia entry for the algorithm.
Note that the implementation does not follow a good naming convention and uses JavaScript functions, eg, charCodeAt
instead of relying on its standard library (Dust's standard library is missing, it works by transpiling Dust code to JavaScript). There is no []
operator to access array element currently; as a workaround, JavaScript Array
prototype has been armed with get
and set
functions which facilitates the usages like T.get
in the example.