Rust, again

I think I got the fundamentals now. But I still lack proper practical Rust experience on a larger scale. So I was looking at some courses, like the Rust course from Let’s Get Rusty. The guy has some really good videos on youtube. Now he also offers a course, which is basically the videos glued together with an online platform where you can interact with other Rust learners and ask questions. The course is 500 USD and you’ll get some kind of certificate if you finish the exams/projects. My worries there are, first it’s a lot of money, second how ‘accredited’ is this certificate when it comes to looking for a Rusty job. Nevertheless, if these kind of courses are your preferred way to learn, do it, it’s good, the guy knows what he’s talking about.

But I got some more books: “Practical Rust Projects” and “Rust in Action”. Both are very practical. But again, the projects aren’t that interesting. I think in order to persevere, I need some little projects which I personally find very interesting. One is cryptography! Don’t get me wrong, that’s gonna be on a very amateurish level. So I started a little command line program ‘secrets’, written in Rust. It will simply allow the user to en/decrypt messages using various ciphers. At the moment I’ve only implemented a transposition cipher. Next will be the ‘Shamir’s secret sharing’ cipher. I got the idea from Christof Paar’s lectures about modular arithmetic (generally highly recommended!). And I will use the above mentioned books more “on the case” whenever I have to look up a ‘best practice’ to get something specific done in Rust.

The most important thing next to the languange itself is to know some main libraries. So far I got familiar with:

  • clap for writing CLI applications.
  • anyhow for dealing with errors in an application.
  • thiserror for defining your own error types when you write a library.
  • serde pretty cool de/serialisation library.
  • num-bigint for dealing with large integers.
  • regex for regular expressions.
  • imap to connect to mail servers via IMAP.

Also important, how to actually structure your project. Coming from an object oriented view, I like to bundle functionality in small, overseeable files. This is possible with Rust too, although most easy beginner examples always show max two files, the lib.rs and the main.rs. But if I have different objects (Structs) I tend to put them in their own .rs file. Not just Structs, also other “bundles” of common functionality. In Rust that’s called ‘Modules’.

For example for the ‘secrets’ application, I added all functionality for the transposition cipher into the file transpose.rs:

pub fn encode(message: &str, password: &str) -> Result<String> {
    ...
}
pub fn decode(message: &str, password: &str) -> Result<String> {
    ...
}
...

(I should actually also create a Trait (equivalent to Interface in other languages) for these methods)

In order to use the code in transpose.rs, I have to add it as module to the lib.rs:

pub mod transpose;

Then I can use it in main.rs:

use secrets::transpose;

If you don’t use it in main.rs but only in other modules on the “same level” then you don’t need the pub keyword. For larger projects you can also split up modules into different directories. But that’s one of these things I’m not really sure yet what the best practice is.

The bigger things I still have to get a grasp on are:

  • Async - Asychronous programming. Another area which is a huge source of bugs in other programming languages. But in Rust, once it compiles, you’re pretty safe.
  • Livetimes - The general advice seems to be: Avoid dealing with them, if it’s not strictly necessary. But sometimes it is. For example in asynchronous environments.
  • Webassembly - Just curious, interesting area. I’m not a web developer. Maybe that’s something which will get me a bit more interested in that field?

So much for now… :-)