Admittedly, it takes time to get used to this syntax, with the pipe (|) replacing the parentheses. But after overcoming the initial awkwardness, I find it clearer to read than another set of parentheses.
The main difference is that in Rust we have to specify the type (Point). String literals are encoded in Unicode, handling special characters without issues.
And with webpack, we’ll be able to also hide cargo behind NPM run build. With this guide, the normal workflow of a web developer can be retained, once Rust is set up for the project.
Rust has been built from the ground up in recent years, taking into account everything humanity has learned about programming-language design in the past few decades. Pattern matching in Rust is a pet feature of mine.
To say it straight, using a strict type system can feel very cumbersome at times. If you thought the type systems of C++ or Java are strict, then brace yourself for a rough journey with Rust.
But I understand that for beginners, it can be very annoying to fight the compiler all the time. It will install the compiler (rust) and the package manager (Cargo) for the newest stable version of Rust.
Check the installation by typing cargo --version in a terminal. We start with a Rust project that can be compiled into a NPM package.
These are dependencies from crates.Io, the default package repository that Cargo uses. Our final binary will only contain the Web API bindings selected like this.
When it expects a reference to a Value as an argument to a function, it won’t accept a static string. The Rust compiler is smart enough to defer which types are involved in the conversion, since the function signature leaves only one possibility.
I’d suggest you use the exact same versions when following this Rust tutorial. This is a webpack plugin specifically for loading Rust packages built with wasm-pack.
Feel free to adjust these if you prefer a different setup. But now that all’s in place, we can easily extend the Rust code without worrying about any of this.
A red square represents a cell infected with the virus. By clicking on individual cells, you can add or remove infections.
After a fixed number of days of being infected, the cell recovers and is immune for the rest of the simulation. The form on the top right side controls the various simulation parameters.
If the method changes the internal state of the object, it must be signified by explicitly adding smut self in the parameter list. For the return type of function, Rust uses the arrow notation ().
Rust will assign unique numbers to the values, and we, the programmers, need not worry about it. The two dots between the two values create a range of integers that the for loop iterates over.
Thus, the current variable is of type InfectionStatus, and we can use Rust ’s powerful pattern matching on it: If the cell is healthy now, we compute the chance to get infected today in a separate function that will loop over all neighbors in the configured radius.
First, whether the cell dies today, again based on a random number generated on the spot. Otherwise, we check if the recovery date has been reached, in which case the new status will be Immune .
Finally, if none of the two checks were positive, then the infection day counter is increased by one. The last option in the pattern matching is that the cell is already dead or immune.
After the match expression, we call the setter function that takes an InfectionStatus value. It converts the value back to an u8 and writes it to the output array.
The whole workload is one huge loop doing the same computation over and over again with only slightly changing values. To investigate a bit further, I performed the same tests on my Samsung Galaxy S7.
You can just open the developer console to read the times you get on your device. Was support for Rust has now reached decent maturity, so it’s ready to start using it for your work.