Skip to main content

Rust, Part One

What is Rust?

In short, a programming language focused on memory safety. Compiled like C/C++. Has a lot of neat features. Let’s talk about that.

Why Rust?

Among other things, Rust is built to be modern. It has its own dependency manager (cargo) which allows you to easily get the modules and components you need.

I used to thoroughly dislike Rust, but that was because I wasn’t approaching it the right way. You see, Rust’s whole philosophy is built around making safe code from the get-go. Evidently, the C++ code I was writing wasn’t safe, at least from Rust’s perspective. So I suppose I ought to explain Rust’s whole approach to memory management.

Suppose you have two variables, each initialized with the same value.

let x = 2;
let y = 2;

Both x and y now own their own respective values of 2. Now, say we do this:

let y = x;

At this point, x may as well have gone out of scope. Rust has a particularly fickle way of dealing with memory, and insists that when you use a variable somewhere, unless you ‘borrow’ it, that variable is no longer of use outside of where it got used. This confused and frustrated me greatly; C and C++ don’t have this problem, because memory isn’t constantly treated like a resource that needs to be tracked (unless you’re doing things with threads, in which case if you’re not using mutexes or semaphores, I would highly recommend looking into how that works).

So, if you wish to give a variable another variable’s value without moving said value, you must explicitly declare so:

let x = 2;
let y = &x;

As a programmer coming from C++ to Rust, this convention is not immediately clear.

Classes

Rust doesn’t have object-oriented support in the ‘typical’ sense (typical meaning having tokens such as class). Much like C, ‘classes’ are simply structs. Coming from a C++ perspective, the inability to define proper classes becomes jarring, but realistically, Rust’s structs are effectively the same thing.

Defining a struct in Rust is somewhat similar to JavaScript, except that instead of defining values when you define the struct, you define the types:

struct computer {
    ram: i8,
    cpu: String,
    gpu: String
};

Then, when initializing an instance (object) of said struct, you can then define the values:

let p = computer {
    ram: 16,
    cpu: String::from("some cpu"),
    gpu: String::from("some gpu")
};

Here’s where we run into another fun (frustrating) aspect of Rust: types.

Types

C++ and like languages don’t usually care how you deal with types because, for the most part, everything’s the same. Numbers can be interchangably compared with other numbers of different types, and strings are not considered different types (unless you’re using C-style strings, but maybe that will be for the next post). The developers of Rust, in their infinite wisdom and endless pedantry, decided that the strings "this" and "this" are not the same if the former is of type &str (a string slice), and the latter of type String (a string object). The difference here comes down to how each is allocated in memory. A string slice, much like a C-style string, is a fixed size allocated on the stack and cannot be expanded or shrunk, only altered in its original size. A string object, on the other hand is allocated on the heap, meaning that you can expand or shrink it as you please, messing around with it without fear of messing around with other memory. This has the practial upshot of ???.

I haven’t even gotten into the different integer types in Rust. There are separate bit sizes for each amount of memory you could want to allocate to a given integer variable. I presume this is a memory-saving measure, since sometimes you don’t need to use 16 bits just for one number. However, this has the effect of making my life harder because I just want to type int when specifying a struct data member’s type. Wah.

Comparing data between variables of different types is only a slight hassle, if only because you have to explicitly cast one variable to the other’s type. Most other languages make some base assumptions about comparing types, but as Rust wants to be safe and not cause bugs that can occur in these situations, it forces explicit casting.

That’s about all I have to say for Rust for the time being. Since I started writing this post, I moved on to building a website using Node. Keep an eye out for a post about that.