💾 Archived View for mediocregopher.com › posts › learning-rust-notes.gmi captured on 2024-08-25 at 00:01:35. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-08-18)

-=-=-=-=-=-=-

Back to All Posts

How to Learn Rust

As a Go Developer

(The premise of this post is heavily "inspired" by Ian Henry's How to Learn Nix post series, which I found incredibly fun to read as an intermediate Nix developer.)

How to Learn Nix

I have been a Go developer since even before Go 1.0 was released. I was immediately taken by the simple but useful type system, strong standard library, concurrency primitives, and minimal feature-set of the language. It reminded me greatly of Erlang in those respects, with the added bonus of compiling to a binary on virtually any platform.

Learning Rust has been a perpetual item on my todo list. I've actually tried to do so a couple of times. The first time I tried was so long ago, and Rust was so new, that the hello world example didn't compile correctly due to nightly changes (or something like that). The second time was somewhere around 2015. Things had stabilized, but I still got the feeling that the language wasn't in a finished state, and for such a big language I felt like it would be better to wait some more before investing into it.

Since then there has been somewhat of a friendly rivalry between Go and Rust developers. Or maybe between just me and Rust developers. I have strong opinions about programming languages, and I'm not afraid to share them. In any case, Rust is a point of contention for me.

But now it's almost 2023, and I'm really out of excuses. Over Christmas break, I am learning Rust.

My first step in this journey is to read through The Rust Programming Language book. Or guide. Whatever it is. And I'm going to document my thoughts here as I do so.

The Rust Programming Language

My purpose here is *not* to just dunk on Rust. I *will* be pointing out things I dislike as I come across them, but many of these will also apply to Go and other languages which I use. The things which do only apply to Rust should be considered as me ~~whining~~ venting.

My purpose here is also *not* to criticize the authors of this book. I *will* be noting things which I find confusing, which may be helpful for someone who has forgotten what it's like to not know Rust.

My purpose here is, mostly, to have some fun, and keep myself engaged while I read through this thing. I will likely never look at these notes again, but I usually don't ever look at my notes again. The act of writing them is the most helpful part.

So, without further ado...

Ch 1. Getting Started

Hello world and baby's first cargo project.

Ch 2. Programming a Guessing Game

Ch 3. Common Programming Concepts

Ch 4. Understanding Ownership

Property is theft, eat the rich.

    let s = String::from("hello");  // s comes into scope

    takes_ownership(s);             // s's value moves into the function...
                                    // ... and so is no longer valid here

// ...

fn takes_ownership(some_string: String) { // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

It makes sense *why* this rule is in place, it means the compiler doesn't have to keep track of what happens to the value after it's been passed to the function, which simplifies things. It just makes me uncomfortable...

Ch.5 Structs

Phew, back to easy street.

Nothing super surprising about this chapter. I still find it weird that the scope of a variable changes depending on its type, but that's not directly to do with structs.

Ch 6. Enums

Nothing really crazy in this chapter either. `if let` seems convenient.

Ch 7. Modules and Crates

I'm trying to figure out what the benefit is for having these `mod` directives, when they have to map 1:1 to a filesystem anyway. Would it be at all different to just say that all module paths starting with `crate`/`self`/`super` correspond to the filesystem tree, and provide a way for modules to declare themselves public or not?

I guess that's inconsistent with how other things are defined, if you consider that the contents of the file comprise an anonymous module, then its name and public-ness need to be defined external to it. But since the name of the file corresponds to the name of the module anyway, all that's missing is a naming convention for public-ness (files starting with `_` are private, or something like that) and you could ditch `mod` and stay within the existing pattern.

Ch 8. Common Collections

Ch 9. Error Handling

I suppose `if err != nil` isn't going to make an appearance in this section.

Ch 10. Generic Types, Traits, and Lifetimes

Ch 11. Tests

Nothing really to say here, looks usable.

Ch 12. Example Command-line Program

My enthusiasm is waning, skipped.

Ch 13. Functional Language Features

Really burying the lead here!

The iterator issue will be interesting when I come to use it, though I'm used to a language which doesn't have an iterator type at all! So I suspect I'll make do.

Ch 14. More about Cargo and Crates.io

Skipped.

Ch 15. Smart Pointers

Implicit deref coersion, your time is now!

Fin

There's still a couple chapters to go, but at this point I feel like I've got a good enough handle on the language to start actually using it, and I'm excited to do so. Things like concurrency and advanced pattern matching can wait.

Overall, I would describe my emotions as a mix of excitement and intimidation. I'm excited to expand my horizons a bit, and get into the weeds of a complex type system, but there's also just a lot of new things to know. New idioms, new libraries, new gotchas. And it's just a big language to start with.

Project number one: rewrite the ginger interpreter!

-----

Published 2022-12-23