As a Scala engineer working mainly in the JVM for a while, I keep seeing people in my field switching to or at least also learning Rust.
I get the appeal from a FP perspective, so I thought to at least go through the book Programming Rust and try to do most of the examples.
This writing is more a way to organize my thoughts, but maybe some will be useful for other Scala engineers trying to learn the language.
The idea is to write small updates on things that I’ve learned or that work differently then writing FP code in Scala.
Installing Rust
On my Mac I normally use homebrew to install everything, but I’ve noticed that rustup
is the preferred way to run the tool.
I still used brew install rustup-init
to not totally ditch Homebrew, but from now on I’ll use the rustup
command to manage the Rust dependencies.
Unit Tests
Coming from JVM I automatically created a tests
sources folder to add the unit tests for my application, but the way this is intended in Rust is to add the unit tests to the file you are writing the functions in. This also allows private access to functions.
This is the way I have it currently setup:
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
assert!(a != 0 || b != 0);
while b != 0 {
let t = b;
b = a % b;
a = t;
}
return a;
}
#[cfg(test)]
mod tests {
use super::*…