Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for The Rustlings exercises - part 2
Nicolas Fränkel
Nicolas Fränkel

Posted on • Originally published atblog.frankel.ch

     

The Rustlings exercises - part 2

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

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 theJobStatus structure into aMutex, just as thedocumentation describes.Mutex represents a global lock around an object. Please look at the above link for more details, which does a great job explainingMutex in depth.

Macros

This section focuses onmacros.

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 ofmacro3.rs is that the code defines the macro in a dedicated module. The solution is more straightforward than what I tried first, namelyuse and prefix the macro with the namespace. You need to annotate the macro with#[macro_export]. Again,RTFM.

I couldn't solvemacro4.rs without hints. I was missing the correct separator between arms. I tried previously with commas, like formatch, but it failed, so I wrongly ruled the separator out.

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

Clippy

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

Microsoft Office's 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.

--More Lints with Clippy

You install Clippy with the following command:

rustup component add clippy
Enter fullscreen modeExit fullscreen mode

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

cargo clippy
Enter fullscreen modeExit fullscreen mode

I believe Clippy should be mandatory on all projects.

In IntelliJ IDEA, you can configure Clippy by going to menu:IntelliJ IDEA[Preferences] and then menu:Languages and Frameworks[Rust > Cargo].

Conversions

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

&str and String back and forth conversion functions

Also, I learned that you need tocollect() into aVec<&str> after you'vesplit() aString.

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!          |+--------------------------  ------------------------+                          \/     ▒▒          ▒▒▒▒▒▒▒▒      ▒▒▒▒▒▒▒▒          ▒▒   ▒▒▒▒  ▒▒    ▒▒        ▒▒  ▒▒        ▒▒    ▒▒  ▒▒▒▒   ▒▒▒▒  ▒▒  ▒▒            ▒▒            ▒▒  ▒▒  ▒▒▒▒ ░░▒▒▒▒░░▒▒  ▒▒            ▒▒            ▒▒  ▒▒░░▒▒▒▒   ▓▓▓▓▓▓▓▓  ▓▓      ▓▓██  ▓▓  ▓▓██      ▓▓  ▓▓▓▓▓▓▓▓     ▒▒▒▒    ▒▒      ████  ▒▒  ████      ▒▒░░  ▒▒▒▒       ▒▒  ▒▒▒▒▒▒        ▒▒▒▒▒▒        ▒▒▒▒▒▒  ▒▒         ▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▒▒▓▓▒▒▒▒▒▒▒▒           ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒             ▒▒▒▒▒▒▒▒▒▒██▒▒▒▒▒▒██▒▒▒▒▒▒▒▒▒▒           ▒▒  ▒▒▒▒▒▒▒▒▒▒██████▒▒▒▒▒▒▒▒▒▒  ▒▒         ▒▒    ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    ▒▒       ▒▒    ▒▒    ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    ▒▒    ▒▒       ▒▒  ▒▒    ▒▒                  ▒▒    ▒▒  ▒▒           ▒▒  ▒▒                      ▒▒  ▒▒
Enter fullscreen modeExit fullscreen mode

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 on thework branch:

GitHub logo ajavageek / rustlings

🦀 Small exercises to get you used to reading and writing Rust code!

All Contributors

rustlings 🦀❤️

Greetings and welcome torustlings. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!

...looking for the old, web-based version of Rustlings? Tryhere

Alternatively, for a first-time Rust learner, there are several other resources:

  • The Book - The most comprehensive resource for learning Rust, but a bit theoretical sometimes. You will be using this along with Rustlings!
  • Rust By Example - Learn Rust by solving little exercises! It's almost likerustlings, but online

Getting Started

Note: If you're on MacOS, make sure you've installed Xcode and its developer tools by typingxcode-select --install.

You will need to have Rust installed. You can get it by visitinghttps://rustup.rs. This'll also install Cargo, Rust's package/project manager.

MacOS/Linux

Just run:

curl -L https://git.io/rustlings| bash# Or if you want it to
Enter fullscreen modeExit fullscreen mode

To go further:

Originally published atA Java Geek on June 27th 2021

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Dev Advocate | Developer & architect | Love learning and passing on what I learned!
  • Location
    Geneva
  • Work
    Developer Advocate
  • Joined

More fromNicolas Fränkel

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp