Nonsense from My Brain to Your Eyes

Programing Languages

A Few Weeks of Rust

Tags: rust

Rust

It was a lovely warm summers week. The sun was shining, the geese where honking, a side of morris dancers was jingling down the street, and as I tend to do when on I have a week off work, I was inside learning Rust.

Rust is a functional language that puts a lot of effort into trying to guarentee memory safety, through some rather clever rethinking. While being a functional language, it can kind of, sort of, maybe if you squint a bit, do some objecty stuff as well.

This is just a quick overview of my learnings and feelings on Rust. Not an intense overview of the language.

Learning Resources

Rust has one of the best beginners tutorials I've come across in a while. "The Book" is a well thought out, and well written book. It's also available in print, though as all programming books, a little bit pricey.

I started on The Book using the online edition, but quickly wanted something I could go sit out in the big blue room and flick over some chapters that hadn't quite sunk in yet.

The Rust Community is also very welcoming. They now use Discord for chat, but there is a lot of chatter about on Twitter and around the net in general. They all seem very welcoming and happy to talk through problems with new people. The only other communities that I've been in that felt this relaxed and welcoming was the early Ubuntu community and the Python community.

Tools Tools Tools!

IDE

As programming ecosystems go, Rust is still quite young, but really very healthy. There don't seem to be an dedicated IDE's around yet, but many of the multipurpose IDE's have built integrations. I ended up using Visual Studio Code, as I use it for React development and was a little familiar with its feature set. The integration with Rust is pretty well done. Intellisense like code completion all worked nicely, and it tried to spot your mistakes early, giving you the little red wiggly line of learning. It also integrated with Cargo, rusts package manager, to tell you if you where using the latest version of a crate or let you select a version from a crate in your Cargo.toml configuration file. Which is really quite neat.

Cargo

Cargo is the entry point for new Rust projects, grabbing, searching and building Crates (packages) and also a convenient access to the compiler.

  • To start a new project you run cargo new <project name>.
  • To build a project, run cargo build
  • To build and run a project, run cargo run
  • To run unit tests, run cargo test
  • To search for a Crate run cargo search <some search params>
  • To install a Crate run cargo install <crate name>

So as you can see, its your general purpose everything.

When you create a new project, it will generate a hello world type rust file in a src directory and a Cargo.toml file. It will also initialise a git repository for the project.

[package]
name = "simplefind"
version = "0.1.0"
authors = ["matt"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
structopt = "0.3.2"

Much like Pythons setup.py this file sets the packages metadata and lists the dependencies. When you cargo install a crate, this file will be updated for you. I have to admit, cargo is everything I want pip to be and more. It's nice to have it set style and layout of projects from the get go.

The compiler

Rust handles running this for you most of the time, but I really just want to point out how incredible the error messages from it are.

error[E0596]: cannot borrow immutable borrowed content `*some_string` as mutable
 --> error.rs:8:5
  |
7 | fn change(some_string: &String) {
  |                        ------- use `&mut String` here to make mutable
8 |     some_string.push_str(", world");
  |     ^^^^^^^^^^^ cannot borrow as mutable

I mean, it tells you whats wrong in plain English and suggests how you can fix it. If only all compile time errors where this lovely.

The Language

The majority of languages I work in these days are untyped languages, and very much object orientated languages. Messing around with a functional language like Rust after some time with those is simultaneously refreshing, enlightening and frustrating.

Refreshing

Partly due to the typing system in Rust and partly because of its functional nature, it makes you code in a way which can make the logic a lot easier to see. Having typed variables also makes this simpler as it's easy to know what a variable may contain. The compiler itself can tell you if you're doing some daft things like treating a &str as a float.

Enlightening

Rusts Ownership and Borrowing rules are really incredibly enlightening. The whole concept makes it really easy to write thread-safe code. It takes a little while to bend your head around, but once you start getting it, it does make a lot of sense. If I've taken anything back to my day to day programming, it's been trying to think of the world with similar rules to these.

Frustrating

Having spent so long inside fully Object Orientated languages, coming back to functional stuff is kind of frustrating. I often find myself still thinking in the "I need a class with the following attributes" rather than "This can be an enum, then apply the following traits". I do miss things like argparse, the decorated macro equivalent in Rust, while quite powerful, can be a little awkward to use when you're not used to it. Having said all that, most of that is just spending time in the language and bringing my mind back into that way of thinking about the problems I'm trying to solve.

A quick note on Ownership.

Rust has a few simple rules on references:

  • At any given time, you can have either one mutable reference or any number of immutable references.
  • References must always be valid.

Since all of your variables are immutable by default, only being mutable if declared with the mut keyword, these don't generally give you much issue. Each variable is only valid for the scope it is, but another scope can borrow a reference to it, as long as the above rules are maintained. Its a pretty neat concept which took my brain a little while to get used to, but it makes your code a lot cleaner.

In conclusion

Rust is a pretty neat language. Any time I can I will certainly be trying to use it more and find more contexts where it's safe to be used :)