/ RUST, EXERCISES, PRACTICE, LEARNING

The Rustlings exercises - part 2

Last week, I dived into the Rustlings exercises. It was a bit long, and I decided to split my understanding into two different posts. Now is the time to finish them.

This is the 4th post in the Start Rust focus series.Other posts include:

  1. My first cup of Rust
  2. My second cup of Rust
  3. The Rustlings exercises - part 1
  4. The Rustlings exercises - part 2 (this post)
  5. Rust on the front-end
  6. A Rust controller for Kubernetes
  7. Rust and the JVM
  8. diceroller, a sample Rust project
  9. Rust’s Vector

Threading

There’s a single exercise for threads. It’s a bit disappointing because it’s a weak point of mine: I’d have loved more practice.

The solution is to wrap the JobStatus structure into a Mutex, just as the documentation describes. Mutex represents a global lock around an object. Please look at the above link for more details, which does a great job explaining Mutex in depth.

Macros

This section focuses on macros.

In general, I’m afraid of languages that offer macros. I think that macros decrease readability. Most macros are just a way to avoid duplicating code: other approaches achieve the same goal without the readability issue. Because Rust compiles to native code, most alternatives are not available. Hence, macros become a must.

The main problem of macro3.rs is that the code defines the macro in a dedicated module. The solution is more straightforward than what I tried first, namely use and prefix the macro with the namespace. You need to annotate the macro with #[macro_export]. Again, RTFM.

I couldn’t solve macro4.rs without hints. I was missing the correct separator between arms. I tried previously with commas, like for match, but it failed, so I wrongly ruled the separator out.

The point of quizz4.rs is to (finally) write a simple macro. It’s pretty straightforward with the example of the previous macros files. My issue laid in how to concatenate &str. You first have to own the left operand with the usage of to_owned().

Clippy

This series of exercises is pretty straightforward. But it allows us to know about Clippy.

clippy

No, I’m not talking about Microsoft Office’s assistant from the 2000s.

The Clippy tool is a collection of lints to analyze your code so you can catch common mistakes and improve your Rust code.

You install Clippy with the following command:

rustup component add clippy

From that point on, you can use Clippy like this:

cargo clippy

I believe Clippy should be mandatory on all projects.

In IntelliJ IDEA, you can configure Clippy by going to IntelliJ IDEA  Preferences and then Languages and Frameworks  Rust  Cargo.

Conversions

The exercises related to conversions allowed me to sum up the available functions that convert between &str and String.

&str and String back and forth conversion functions

Also, I learned that you need to collect() into a Vec<&str> after you’ve split() a String.

Conclusion

And that’s a wrap! I cannot resist the temptation to show the final reward that unlocks when you’ve completed all exercises.

๐ŸŽ‰ All exercises completed! ๐ŸŽ‰

+----------------------------------------------------+
|          You made it to the Fe-nish line!          |
+--------------------------  ------------------------+
                          \/
     โ–’โ–’          โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’      โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’          โ–’โ–’
   โ–’โ–’โ–’โ–’  โ–’โ–’    โ–’โ–’        โ–’โ–’  โ–’โ–’        โ–’โ–’    โ–’โ–’  โ–’โ–’โ–’โ–’
   โ–’โ–’โ–’โ–’  โ–’โ–’  โ–’โ–’            โ–’โ–’            โ–’โ–’  โ–’โ–’  โ–’โ–’โ–’โ–’
 โ–‘โ–‘โ–’โ–’โ–’โ–’โ–‘โ–‘โ–’โ–’  โ–’โ–’            โ–’โ–’            โ–’โ–’  โ–’โ–’โ–‘โ–‘โ–’โ–’โ–’โ–’
   โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“  โ–“โ–“      โ–“โ–“โ–ˆโ–ˆ  โ–“โ–“  โ–“โ–“โ–ˆโ–ˆ      โ–“โ–“  โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“
     โ–’โ–’โ–’โ–’    โ–’โ–’      โ–ˆโ–ˆโ–ˆโ–ˆ  โ–’โ–’  โ–ˆโ–ˆโ–ˆโ–ˆ      โ–’โ–’โ–‘โ–‘  โ–’โ–’โ–’โ–’
       โ–’โ–’  โ–’โ–’โ–’โ–’โ–’โ–’        โ–’โ–’โ–’โ–’โ–’โ–’        โ–’โ–’โ–’โ–’โ–’โ–’  โ–’โ–’
         โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–“โ–“โ–“โ–“โ–“โ–“โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–“โ–“โ–’โ–’โ–“โ–“โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’
           โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’
             โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–ˆโ–ˆโ–’โ–’โ–’โ–’โ–’โ–’โ–ˆโ–ˆโ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’
           โ–’โ–’  โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’  โ–’โ–’
         โ–’โ–’    โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’    โ–’โ–’
       โ–’โ–’    โ–’โ–’    โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’โ–’    โ–’โ–’    โ–’โ–’
       โ–’โ–’  โ–’โ–’    โ–’โ–’                  โ–’โ–’    โ–’โ–’  โ–’โ–’
           โ–’โ–’  โ–’โ–’                      โ–’โ–’  โ–’โ–’

Thanks to all who contributed to build Rustlings; this is a fun experience: I recommend anybody who wants to learn Rust to have a try at them.

The complete source code for this post can be found on Github.

To go further:

Nicolas Frรคnkel

Nicolas Frรคnkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Currently working for Hazelcast. Also double as a trainer and triples as a book author.

Read More
The Rustlings exercises - part 2
Share this