Throw it in Reverse

My old-guy approach to my first Rust Exercism exercise – and some Rust basics

Parked car upside down on roof
What!? I’m in my spot! Now lemme find some change for the meter!

Exercism starts me with one of many easy problems they have in their Rust track to solve: write a function that, provided a string, will return that string reversed. So, just flip it!

My ancient C upbringing shines at this point – my first inclination is to loop through the characters in the provided string, in reverse order, and append each char to a new string. So, how to do that in Rust? I end up with this:

pub fn reverse(input: &str) -> String {
     let mut rev_str = String::new();
     for byte in input.chars().rev() {

Here, reverse is defined as a (public) function that will accept a string as a parameter (named input) and return a string to the caller. It creates a new string variable rev_str to hold the reversed string while we build it, and then iterates over a loop of the input string’s characters, in reverse order, and pushes each one onto our rev_str variable, and returns it at the end.

Strongly Typed – Really Slam Those Keys

Firstly, Rust is a statically and strongly typed language; if the compiler can’t figure out what type to make a variable, it requires you to specify the type. We also need to remember that all Rust variables are, by default, immutable – once their value is set (at instantiation) it cannot be changed. Anyway, we identify the variable type for our parameter coming in, our holding variable within the function, and our return value. But all three look different, you say?

The easiest is the return value, simply a String; a fancier version of the primitive str type. Our holding variable is the same type – it has to be, it is the variable we end up returning – String::new() just means we are asking for a new, empty String type to stuff our characters in (ALL variables in Rust must be initialized with a default value – this one starts “empty”). The mut you see here is telling the compiler we want a mutable variable – since we will be changing the value as we loop. The incoming parameter however, introduces us to Rust’s use of Ownership, Borrowing, and Lifetimes. Basically, our input param is “borrowing” the variable coming in (passed as a reference) and the borrow will go away once our variable goes out of scope.

Roller coaster with loop in the distance, on hill

Notice we didn’t specify a type for our looping variable byte. Rust forgives us – it can easily identify that byte needs to be a char type because that is what input.chars() will be returning (calling rev() just reverses the order).

Finally (pun intended), if Rust sees an expression with no trailing ; it uses that as the return value and ends the function. You can still specify return – for instance, if you are in the middle of a function and need to exit early.

What I will find out though, when I submit this as my answer to the Exercism problem, is that I’m Rusting wrong (and showing my age)!

%d bloggers like this: