I think it was the Ruby Rogues Polyglot programming podcast where I first heard about the Rust programming language, then again I think I may have heard talk before, but that is when I decided to take a look. To cut a long story short I ended up buying Rust for Rubyists and am now trying to learn Rust.
I only bought the tutorial a couple of days ago and so have managed to get to Chapter 6 of 11 chapters. I’m really enjoying it, and wanted to write something about it that could be useful to others going down this route. When I’ve not been sure about something in the tutorial, I’ve explored a little around it to help me make sense of it and I hope it will help others.
The version of the tutorial I purchased is for Rust v0.11.0 but I have v0.12.0 from 30th July 2014 and with the language developing at the rate it is, the tutorial doesn’t match my version. I was going to install the v0.11.0 version of Rust but decided to run with the one I had because it means I will have to dig into the api documents and hopefully get to understand more about Rust, rather than have it all on a plate.
I’m open to be corrected and happy to update this post so don’t be shy.
Using format! instead of to_str …
Steve makes use of
to_str() in the third chapter on FizzBuzz which doesn’t work in v0.12.0. I first hunted around and came across the
format! macro here which does the job nicely. Where the tutorial has
num.to_str() I used
Later on in the same chapter, Steve improves the code by creating this which takes advantage of the ability of Rust to return a value from an
if ... else block setting it up as a parameter in
println!. This is a macro that uses the same formatting traits as
format! but prints the result to the standard output rather than returning it as a value’
to_str() didn’t work I changed it to make use of the
format!() macro like this:
This may sound a bit silly, but when I looked it it there was something not quite right. Both
format!() appear identical and when I have a better understanding of Rust and have a look at it’s source code, I imagine
println!() may well just call
format!() and print the result to the standard output. My
format!() pal just didn’t feel like the right thing to use.
… then deciding to_string was the way to go
I decided to have a good look round the api and lo and behold I found
to_string() in the std::to_string::ToString Trait which is marked as Experimental but so is a lot of Rust. The net result being my use of
format!() I was so proud of finding having been dropped in favour of
I had a look in 0.11.0 and found
to_string() in a different place although I didn’t find
to_str(). I anticipate that as I learn more about the Rust language I’ll understand more about how to find stuff in the api documents. On the subject of the api documents, the latest build of them are way better than the 0.11.0 documents.