I’ve recently been playing around with rust. For some reason I thought that porting simplify-js to simplify-rs would be a good start.

The biggest hurdle I found for starting with rust was the documentation, rust is just too new for a large corpus of 3rd party docs covering the gaps in the official one (which isn’t too bad as official ones go) to have been created. Then most of the library documentation is just auto-generated type annotation which is more or less useless to a newbie. After I started getting the gist of how rust worked I found the source code on github to be the best reference, certain things (e.g. how to implement the basic operators for your custom type, or even that you can) are just not documented anywhere.

I’m coming at this from the perspective of a person who has experience with languages like Python and JavaScript and thus think of types to be a small number of primitive and take closures for granted.

Superficially rust looks pretty similar to JavaScript with the additon of type annotation:

But this is deceiving, the let in rust is not the same as the let in es6

it’s more like const, you can use the mut keyword to make a mutable variable.

Also if you leave off the last semicolon then the last statement gets returned automatically

The other surprise for JavaScript people is that functions don’t create closures (by default), thus the following will cause an error:

you can use closures when you need to though (ignore the ~ and @ for now)

This is typical of the philosophy of rust a trade off between the ease of JavaScript and the speed of C. Closures while being powerful programming tools do create a performance hit. Not using closures isn’t as big of a drawback as you’d think, simplify-js doesn’t actually use any (it technically does in the define statement but we aren’t porting that).

One of the big differences that rust has compared to JavaScript or Python is it’s type system. The JavaScript version of simplify works on an object with x and y properties , but rust has no such map like object (it does but we don’t want to use it), instead we are going to create our own custom ‘Point’ type, which is handy as that exact type is used in a lot of examples, so

and bam we’re done right? Actually first to unpack that, “struct” defines a 'structured’ datatype, and pub means it can be exported to another module (you’ll see why latter), and no we’re not done, we needed to give the type methods, you use the impl keyword to add methods to a type (you’ll see why that keyword in a second), and the extra @self argument (just like python but with a confusing ampersand an ampersand to pass it by reference, as opposed to by value) for defining methods like so:

so if you have point 'p’ with an x of 5.5 and a y of 1.5 then I can call p.sum() and it will return 7.0, or I can call p.add(0.5) and it will return a new point with an x of 6.0 and y of 2.0. The keyword is called impl because you can implement 'traits’ that other types and functions use, so much like JavaScript objects can implement a method called 'toJSON’ if it wants a custom way to turn the object into json, rust formalizes this so we can implement a toJson method for our point type, which is good because we are transforming our data to and from json

In this case we are actually transforming the Point into a GeoJSON style [x,y] array. Also we havn’t covered the basic type that JSON is, which is an enum, but sufficient to say we are turning into the List subtype of the Json type.

But traits are much more powerful then this, the equality Eq trait covers equality with two sub traits eq and ne, if we implement them then we can do PointA == PointB and PointA != PointB:

Note: from looking at the source it seams that in the current trunk of rust ne is not required for != as it just looks takes the inverse of the eq call.

The last thing we can add is the basic operators, so adding these traits allows up to do something like PointA + PointB

Why? because it then lets us turn this JavaScript:

Which turns into literal rust as:

Into this:

which we can just inline.

OK that’s all for today folks, join me next time when I talk about…some other aspect of rust which I haven’t decided yet.

update: JavaScripter and Pythonista are horrible words and have been removed