Get the books

There are many online books about Rust. Here is my top 5:

  1. The Book - start here
  2. Rust by example - the same concepts as in The Book but oriented more on practical examples
  3. Cookbook - about using crates in real life problems, solutions, best practices
  4. Rustonomicon - when you are ready for something spicy
  5. Learning Rust - back to origin

The following books were harder to find but nevertheless their content is golden

  1. The Embedded Rust Book - Rust for solving tasks with microcontrollers
  2. Easy Rust - if you find The Book confusing (as me) then try this

About the book

What I don’t like about Rust Book is that it’s not touching any real world examples. It goes into long paragraphs to explain some concepts like for kids, then it tries to explain some new ideas on abstract examples. But these examples not created with minimum code in mind, instead it tries to convey a deeper idea. Let's take for example references and pointers. How to make a list? Why all these types assume that my data is immutable? How to change data?

In the book they basically showed how to make a pointer to an integer. And done. How do I do it with structs? My new function returns a Box? Or I create an object first and then wrap it in the Box?

Maybe it’s so obvious and I'll find answers on StackOverflow.

Oh to use pointers for complex structures you need to define traits. Makes sense. And the book will explain how to write Defer trait that is helpful for Boxes, but it will not use Boxes, it will create MyBox that essentially misses all Box functionality. About this abstraction I’m talking here. In order to understand how I need to write a Defer trait, I must read through all paragraphs where they explain why it’s so great to create your own MyBox that doesn’t allocate on heap and create Defer trait that has nothing to do with complex data again.

You created everything with Rc because it made sense and now you want to work with threads? Then change everything to Arc.

Have a fun project

It's never late to get a grasp on another programming language

One cheesy project pop into my head—I must do it. I believe that it boils down into several small tasks. Think about each of them as solving kata. I opened replit and start experimenting

At the first day I learned basic types and control flow.

I installed Rust on Windows, first time in years updated MSYS2 retrying many times until I updated the keyring, I installed cairo, and when I decided to tweet about it, Twitter went down!

... a few weeks later ...

I finished my project. I like how the code looks. I was afraid that it will be mix of Ruby with Java call chaining, but it actually look very neat. HTTP requests do not take many lines, it's far from monstrosity in Java. All json conversion back and forth is seamless.

Prepare the environment

rust mingw w64 config

I changed the editor's theme to One Dark Pro for more comfortable type deduction highlights.

Practice, practice, practice

Note: I totally understand that usage of screenshots of code is a total disrespect, but content was intended specifically for superficial acquaintance. At the time of delivering I was a total newbie. But I might review this article letter in the format of educational post.

Converting C code to Rust. Every cairo_ call is unsafe and requires raw pointers. I should probably chose another crate, because THIS betrays all Rust principles

Refactoring Rust slide 1

The cairo-rs crate requires some features to be enabled. Like this:

[dependencies]
cairo-rs = { version = "0.15.12", features = ["use_glib", "freetype", "png"] }

cairo alternatives

Well, let's see if I can tidy up this function.

Refactoring Rust slide 2

C/C++ cairo examples

It has one call after another that can return an error (like exceptions in other languages, but these are silent). Type vec<> might look similar to generics in Java. On this slide it's a type definition. Tricky one: 2D array, one dimension is dynamic vec, allows to store arbitrary number of fixed-length (7) arrays that may contain datetime value.For generics they (Rust and Java) both use angle brackets <>. The generic is used on the next slide Result<>

Refactoring Rust slide 3

I use the match blocks (similar to the switch blocks) to check for error conditions and to act accordingly. In another language I'd put everything into one big try-catch thing. It's like: close your eyes and run through a minefield.

This looks better, but it doesn't work 🙂 The previous variant also didn't work if directory already exists. You are puzzled? Me too.

Refactoring Rust slide 4

Refactoring Rust slide 5

Extracted 2 functions. Return bool as I would to in C/C++

Refactoring Rust slide 6

But proper Rust-ish idiomatic approach would be to return Result<bool>. Also before we do that look at the 2 new functions

Refactoring Rust slide 7

match doesn't look great, and I will use or_else and map to write straight forward code in Rust style

Refactoring Rust slide 8

Topics

  • Rust cheatsheet ?

Raw pointers

Async

Iterators

Trying to finish up with current Rust project, learning about iterators. Getting my first overflow. Looks pretty dramatic

Iterator in Rust

I added the Copy closure type ("+ Copy" after type definition). It helped. But it didn't help to my understanding. I'll keep it in the back of my mind with all other strange things that I didn't get yet.

Iterator in Rust

the tutorial about the Iterator trait by @LudwigStecher. It's written very clear 👏 Though I'm looking forward to coroutines being implemented in Rust

Troubleshooting

Okay, I have a problem. I found a question on StakOverflow. And the answer is great and it makes total sense. But… But it doesn't make sense, because it's not working 😵‍💫

Result<()> throws "expected 2 type arguments - accepted answer

That's why I spend 30 minutes writing my own investigation 🤓 risking to lose my precious carma if someone will smack the downvote button 😨 Plus I'm only like one week into Rust, thus I don't feel like an expert

Rate this page