The Mystery of Rsplit

Tags: development, technology, rust

AOC 2022, wait what?

That’s right, I didn’t forget to publish this 2 years late; I’m actually going backward in time. I’m tackling the previous year’s AOC to get to know a new language.

RUST

🦀

I know, hold your applause.

Ok Mr. Cool Guy, What did You Actually Learn

This standard library feature I found isn’t actually ground breaking. I’m sure other languages have it, but I got stuck doing it the wrong way.

I wanted to gather parts of a string into a set of “columns,” which I was using vectors to do. Unfortunately the input for this puzzle doesn’t come in columns, it comes in newline-delimited-rows.

Easy peasy, in python I split the string into rows and columns then reverse iterate over the list. I’ll do that in Rust!

for line in block.split("\n").rev() {
    // do my thing!
}

for all 5 of you that plugged that into your text editor, you’ll see the squigly. For the 1 of you that put it into nvim, hey there, it’s pretty great right?

BUT WHY?

The ~Club~ Compiler is a Comply-er

It’s because &str doesn’t have a DoubleEndedSearhcer, obviously.

That’s all, hope this was helpful and next time I’ll be talking about…

Oh, wait that doesn’t tell me anything! Well maybe it does, but now I’m officially stuck.

Well, it turns out it’s because what I was asking to do was not very smart. What I was asking to do was probably realise the whole string (no matter how long, even if it crashes the program) split it up on newline, then reverse that. For strings Rust doesn’t implement it, and StackOverflow is why, also StackOverflow says why. O.K. so it’s not “stack overflow.”

I’m familiar with JavaScript and Python, in which you’d do such a thing. JavaScript and Python are not known for their memory safety.

Rust’s compiler is notorious for its strictness. Coming from Python and JavaScript, you’ll be surprised to see that there’s a compiler! With C# and Java, the story is more that I’m used to “if it can work, it’ll build,” rather than “if it’s not saying it’s right, it’s wrong.” This comes up in Typescript all the time, but since Typescript isn’t actually executed, you can ignore it and let JavaScript handle it for you. Surely, that will be a good time.

Ok, so what did I learn?

Rust is very picky, and I’m having a really good time learning how it handles even the simple things. I’m hoping I can get through Advent Of Code 2022 using it!