Scott O'Brien

Projects, Ramblings and Resources of my (online) life


2022-04-27 00:00:00 +0000 UTC

Why learn Rust?

In Network Engineering at Facebook Meta, we’ve been going through an interesting transformation in which we’re starting to be discouraged to write new services in Python, and, instead start writing more in Rust (and sorry Golang, of which we’ve already got a bit written in.

I’ve always said that I’m not overly a smart person (though am stubborn enough to solve most problems), and don’t expect to be able to pick it up quickly, I decided to start learning by challenge tasks, do as much as I can in it, and document some here.

For my challenges, I’m going to use both Project Euler and compare my solutions in Python and Rust. Python because it’s my most comfortable language.


Multiples of 3 or 5

This one threw me slightly. Nice and easy to play with iterators, but I don’t get why I needed to give it a type in rust. I’d have thought the range might have been smart enough to infer the type?


print(sum(x for x in list(range(1,1000)) if x % 3 == 0 or x % 5 == 0 ))


fn main() {
    let sum: i32 = (1..1000).filter(|x| x % 3 == 0 || x % 5 == 0).sum();
        println!("{}", sum);

Adjacent Nodes

This one is worth writing out. This problem effectively has a 2D array, and asks us to lookup if the value in a given position is a 0, or 1. It should be super simple, but I spent many hours on learning about ways of going about doing this. Some observations


Rust: This one got me very, very confused. Some observations:

  • This was my first attempt
    • I couldn’t figure out how to pass the arrays of variable size through to the is_adjacent function, so I instead used Vectors. Not sure if this is accepted practice considering I probably don’t need the overhead of ever adding/removing items in the constant matrix here
    • The code did not use any errors, but avoided run-time bound checking panics by making good use of the get call.
  • This was my second attempt.
    • This is interesting, that we have an Optional return type, and force the users to read the docs to know what the return behavior will be (I guess no different to returning errors and docs specifying what errors can be returned)
    • Made use of the ? operator to be able to filter errors right back up to a Null return type.