Learning Rust - First Steps

I’ve tried the Rust programming language a few times. There are many features that appeal to me, but I’ve never had a real use case for it in my work, or in my hobby projects. However, recently I’ve been wanting to dive into the world of VST audio plugins. Most plugins are developed using C or C++, but I thought Rust would be a good choice for me as it’s something outside of my experience.

This post describes the first part of the adventure - Getting to know Rust with some practical experience. This is something I’ll need to do before diving into actual VST programming with Rust. I’ll be using:

  1. The Rust toolchain on MacOS

  2. JetBrains CLion, as I’m very familiar with the JetBrains products.

Rustlings

The Rustlings tutorial allows you to get some practical experience with Rust by modifying some examples to make them compile and to get the tests to pass. This is somewhat similar to the Kotlin Koans (which are wonderful!), and it is a good compliment to reading through some of the Rust books.

Following along with the instructions for setting up rustlings, I need a few things to get started:

  1. An IDE - CLion in this case, with the Rust plugin.

  2. A clone of the Rustlings repository - See https://github.com/rust-lang/rustlings
    I used CLion to clone the repository, but that can also be done on the command line easily.

  3. The rust toolchain - See https://rustup.rs
    I followed the instructions on the rustup page in the Terminal window in CLion.

After installing the rust toolchain, it's time to build the rustlings executable. This is pretty straighforward and introduces you to the cargo tool, which does the build and manages the dependencies.

Rustlings installed successfully!

Rustlings installed successfully!

The easiest way to start working on the tutorial is to run rustlings watch in the terminal window. This will show a compilation error in the first example (exercises/variables/variables1.rs in this case).

The first puzzle - declare a variable!

The first puzzle - declare a variable!

Screen Shot 2021-05-29 at 11.46.58 AM.png

Project view

Okay, so I have to locate this file and modify it to fix the issue. Simple enough. In the Project view I can navigate to the file and get to work.

Double-clicking on the file, CLion tells me I don’t have the Rust toolchain configured, and that it didn’t find a cargo project. I’m not sure if I need a cargo project for these examples, but I’m certain that the Rust toolchain is needed! So, let’s fix that.

Need to set up the Rust toolchain

Need to set up the Rust toolchain

Clicking “Set up toolchain”, I see that CLion already knows about my rust toolchain installation, but it wants to know where the standard library sources are. After clicking the “Dowload from…” button, it seems to be happy with that.

Standard library sources downloaded

Standard library sources downloaded

Okay, so I'll skip the cargo project for now, as rustlings is going to be doing the compiling and such in the terminal window.

In this trivial example the problem is that the variable x is not declared properly. Adding let solves the problem.

fn main() {
    let x = 5;
    println!("x has the value {}", x);
}

In the terminal window, rustlings recompiles the file automatically:

Output:
====================
x has the value 5

====================

You can keep working on this exercise,
or jump into the next one by removing the `I AM NOT DONE` comment:

 7 |  // feel ready for the next exercise, remove the `I AM NOT DONE` comment below.
 8 |  
 9 |  // I AM NOT DONE
10 |  
11 |  fn main() {

Deleting the // I AM NOT DONE line allows rustlings to move on to the next example.

So that’s pretty much it! Rinse and repeat from here on out.

I will make some more posts later as I go through the examples, and talk about where I got stuck, or things that surprised me.

Previous
Previous

Learning Rust - The Toolchain

Next
Next

Leverage Part 1 - Open Source