Top 10 things to learn when start working with Rust language

Hamed J.I
3 min readSep 23, 2021

Rust is one of the rising programming languages, and it is famous for its innovative way of memory management.

After about a year of experience in Rust, I believe besides basic topics, these are the most useful topics to learn. Especially if you are coming from another programming language.

I tried to put a link for further detail for each topic.

Ownership and Lifetime

Without any doubt, “ownership” is the most important topic to learn when starting to work with Rust language, Most programming languages like Java, C#, and Python have a garbage collector to free unused memory.

On the other hand in Rust, any defined variable must have a clear lifetime. By clear lifetime, I mean, Rust compiler must know exactly, when to allocate a piece of memory for a specific variable and when to release that memory.

The way that Rust’s compiler works memory, has a great impact on your code and even your design.

Scoping rules — Rust By Example (rust-lang.org)

Strings

Rust has multiple types of strings.

String: It’s very similar to StringBuilder in other languages like Java. The variable content is updatable if required.

&str: This is a reference to a string in memory.

OsString: Operating system string, representing native string for the current running platform.

OsStr: A reference to operating system string.

Strings — Rust By Example (rust-lang.org)

Option

Rust does not have “null”. That means every variable must have a value. But there are some circumstances in which, you need to indicate if a variable is empty or not. This is when “Option” comes into play.

The option can be used as a wrapper for almost any variables to indicate if they have a value or not. It is a kind of equivalent for Python’s “Optional” or C#’s “Nullable”.

Option — Rust By Example (rust-lang.org)

Result

Instead of throwing exceptions, Rust uses “Result” type. “Result” is a wrapper to wrap variables and indicate if it variable value is defined or it has a kind of error. Most of the time “Result” is used as a function return type.

This is somehow similar to Java’s checked exception, which forces developers to handle exceptions at some point.

Result — Rust By Example (rust-lang.org)

Match

“match” is a syntax similar to “switch” in other languages. It can be used like “switch” on variables, strings, enum, “Result” and “Option”. But it has more capabilities in Rust.

Using “match” in Rust language

match — Rust By Example (rust-lang.org)

Channels

Channels are the best way to communicate between threads. It’s like a queue between threads. Objects may be sent by “Sender” from different threads while all sent values can be consumed by a “Receiver” in another thread.

Channels — Rust By Example (rust-lang.org)

Macros

Macro acts like a function that generates code before compilation. In other words, they replace the code just before compilation. Macros are widely used in standard libraries and third parties. They are similar to #define in C/C++ language.

Macros — The Rust Programming Language (rust-lang.org)

Traits

“Traits” are similar to interfaces in other programming languages. A “Trait” defines a set of functions to implement by other structs. A struct may implement multiple traits, And traits may inherit other traits.

Traits: Defining Shared Behavior — The Rust Programming Language (rust-lang.org)

Box

“Box” is a pointer to heap memory. Most of the time, Box is used with traits. As traits object sizes are unknown at compile-time, wrapping them by “Box” helps with ownership and object size.

Box in std::boxed — Rust (rust-lang.org)

Arc

“Arc” is needed for using a variable in multi-threads. Arc can be used as a wrapper for a variable then the “Arc” may be sent to multiple threads.

In other words “Arc” is a kind of wrapper that lets using a single variable in multiple threads.

Arc in std::sync — Rust (rust-lang.org)

Conclusion

As I mentioned if you are coming from another programming language the first and most important thing to consider is understanding Rust memory management and lifetimes.

I chose the “Programming Rust, 2nd Edition (oreilly.com)” book instead of Rust documentation(I read the first edition). For me, it was more fluent to start learning Rust from this book instead of Rust’s documentation.

--

--