Working with Strings in Rust
This post is inspired by Valid Palindrome from LeetCode. It does not contain any solution code.
I’m venturing out from an OOP programming world, and I’ve found that Strings in Rust are a zone of relative comfort.
There is more than one type of string in Rust.
There are two types, and the differences between them are pretty interesting. I don’t think this is uncommon for systems that are so intensely interested in the types of memory that are being allocated.
std::string::String
std -> Crate
string -> Module
String -> Struct
The String class is a Vector of UTF-8 (see the RFC spec for more on UTF-8) characters. That’s important to know because since all the characters are wrapped up in this Vector Class / Struct, we get all the powers of the Vector to use on our series of chars.
Without having put my hands on any micro-controllers yet, this feels like the way I’ll likely manipulate strings if the option is available. It’s comfortable for me, it’s powerful, and you can derive it from the other string type. ((Side note from future me: This is not how you do it in microcontrollers!))
Here’s how accessed the contents of the string, and from within I was able to make evaluations to determine if the string was a palindrome or not. I won’t give the full code to avoid spoilers.
for c in s.chars() {
// do evaluation & addition / removal work on characters here.
}
Being able to iterate each character in a string with a simple for loop is great. From within that loop, I was able to make evaluations, build new strings, and complete the LeetCode Problem.
std::str
I don’t really have a good grasp on this yet, I’ve not made the time to use it just yet. Here is how the docs describe it:
The
str
type, also called a ‘string slice’, is the most primitive string type. It is usually seen in its borrowed form,&str
. It is also the type of string literals,&'static str
.
Even though it’s a primitive type, it’s not just refs to memory. It’s still a sort of structure that wraps a series of utf-8 values because you can still call dot notation style invocations to methods on an instance of type ‘str’.
The list of implemented functions (which seems to be equivalent to class methods) for this type is really huge. I’m not going to cover any of them because the point is that there is a lot of power here.
Rust Built-ins are Special.
To cover the built-in I was so pleased to work with, I need to switch topics over from strings to the primitive type char, as it has one of my favorite Rust built-ins so far.
.is_alphanumeric()
Rust has a built-in special character checker. I love this! It’s simple, it’s everywhere, no import needed. It’s attached right to the ‘char’ primitive. This is so damn thoughtful and makes me happy.
It’s very encouraging because if Rust comes with a lot of stuff like this out of the box, I’m sure there are loads more goodies coming my way as this learning journey continues.
Conclusion
I’m getting more used to the syntax already, but there is still a comfort gap even after 2 weeks of exposure. I love the help I receive from the Rust compiler though, and the distance away from my comfort zone feels like it’s shortening.