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.
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
you can use closures when you need to though (ignore the ~ and @ for now)
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:
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
Which turns into literal rust as:
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.