For the second year in a row, for my birthday, I took some time off work to learn some new things. This year it was Rust. (Last year it was the DHT Kademlia) I enjoyed spending two days with Rust. I didn’t get very far (about 80% of “the book”) but I did get far enough to learn a number of things and come up with a short list for further research. I’m looking forward to getting back to it.

I kept most of my notes on twitter/mastodon;

This thread can be found at toot.cafe includes 39 statuses.

Rust's stack and heap explanation is probably the first one that's stuck in my head doc.rust-lang.org/book/ch04-01

It's got "why" as a well as "what" and that's probably the clinch.

It does get a little out there though

> This type is allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time.

That makes sense kinda? String literals however could be on the stack because we can figure that out in advance? Seems right.

Ok after 6 months I still got the very few basics I had and now I have; more basics. Time to sleep.

I got to get this out of my head before sleep. Whatever &str is to String is going to be interesting. Functions asking for references seem to take objects too. If string literals are slices of the binary of the executable then can I read that too?

Why did rls suggest a dozen traits for the method I was looking to use but not the one I needed? (For Stdout flush)

The kids asleep, everyone is fed, so it's time for more rust!

Modules in The Rust Book were confusing, but Rust By Example has ... good examples!

This explains succinctly how the path lookups work. doc.rust-lang.org/rust-by-exam

The rust book explains how modules and privacy work doc.rust-lang.org/book/ch07-00

Another hour in another hundred more questions.

I skipped ahead from modules to tests; In testing my tic-tac-toe game I've;
- Discovered that the Result objects are more fun than options for validating input because you can return error strings
- panic vs result? Which is more idiomatic for "constructors"?

Modules do make sense I suppose. It's weird to "mod foo" and have it load the "foo.rs" file but not.. "use foo" but that's just my js head talking.

There must be cooler assertions than assert, assert_eq, and assert_ne! However pretty assertions is rad.

github.com/colin-kiegel/rust-p

Not much to see here yet but maybe one day you can play a game.

github.com/reconbot/tic-tac-to

is;

result.expect_err("should have got an error")

the best way to assert an error result enum? I think not. I also don't know how to do it better.

something something heap memory

Oh yeah! I finally understand some of the c++ memory bugs @indutny@twitter.com was helping me through with serialport! I'd use a heap based string in a struct and not free the struct later because I cargo culted other struct's patterns who had a stack based strings.

I probably described that wrong, but at least I finally get what was going on from a bug last year 😅

I just finished the vectors chapter (doc.rust-lang.org/book/ch08-01). My biggest "ahh ha!" moment is that a reference to a value in a vector can have it's value replaced.

Moving onto String and &str, this morning I was wondering when I'd get back to these two.

One of the things that I noticed earlier is that a String ref is treated the same as a string slice ref. I assumed it's cheap or free to make a string slice of an entire string so they just did it for you and behold! It does! with "deref coercion" discussed in chapter 15.

String concatenation takes ownership of the first string, reads a reference to the second string and returns ownership of the first, invalidating all the original references. Why not take a mutable reference?

In my Tic Tac Toe game I copy the board struct and return that whenever you play a move. This means you can keep all your old game states around and use them to undo or debug or whatever. I copy all the values because they're all small scalar values.

I could share read only references between game states for stuff that didn't change if that was a thing in this game or it was some massive game of tic tac toe.

I was considering doing this take ownership and mutate thing because I don't really have to keep old states around. But also why do that when you can take a mutable reference? I couldn't think of a reason and it seems easier. So back to strings, why not take a mutable reference?

format!("{}{}", s1, s2) doesn't take ownership and returns a new string. Still not giving me any clues to "when do you take ownership and return vs a mutable ref", but I like it anyway.

The notes on unicode are good and I'm glad intelligent handling of string is baked in. I do wish it pointed me to some "grapheme cluster" tools instead of saying "look it up" I have no idea how to evaluate a crate before using one.

On to Hash Maps. I have a lot of question with this chapter.

We learn about some vector methods first, and then are told we can take a vector and make it hash but don't show it. Then we learn some new type syntax where we say "hey compiler figure it out!" by using "_" as a type?

Hash's are another example of [] indexing panicing instead of returning a null (which isn't a thing in rust). I love how sharp an edge this construct is.

Huh this chapter gave me homework.

I'm so confused that collect can't make a HashMap out of tuples without cloning them. The compile errors do not help me understand this with my level of knowledge today.

Somehow;

> value of type `std::collections::HashMap<&str, i32>` cannot be built from `std::iter::Iterator<Item=&(&str, {integer})>

Means I use clone on the collection. But afaict I get the same types on the clone. Maybe it's an ownership thing? 🤔

Reading up on doc.rust-lang.org/std/error/tr and wanting to skip ahead to learn about traits.

As an aside, I *love* the run test button in the vscode rust extension. Having a way to immediately run a test is majorly undervalued in my life.

Hey! It's playable!

tic-tac-toe begs for optimization but then you have to weigh it with the fact your play board is 9 cells and it doesn't matter

This bit of code seems to be not optimal. Like I'm probably missing an obviously easier way to do it. My JavaScript is probably showing.

github.com/reconbot/tic-tac-to

But you know, I might be treating references wrong, or not doing things the right way but because it's Rust, I can trust my code to not be doing things I don't expect. That's the biggest, most important part of Rust.

Like my stupid tic-tac-toe game doesn't have memory leaks 😭