Exploring Rust with a Python Background: My Early Experiences

Exploring Rust with a Python Background: My Early Experiences

Background

I have been writing Python code professionally for over half a decade (about 7 years). I have used it in various companies and products, such as Search Engines, NLP, Satellite Data Processing, Machine Learning, and more. It's a great language and has a tremendous and rich ecosystem that is hard to find in other languages, especially if you are in the data world.

That said, Python has some weaknesses that you can't overlook as you grow in your career. One of them is performance and memory management. It's not the worst (I've seen worse, believe me!), but it's not the greatest either. When you expand to scenarios where speed and memory management are crucial, Python is not the one to call.

Like most software engineers throughout their careers, I have written code in different languages, such as JS/NodeJs, TypeScript, and Golang. I was able to see how different languages somewhat translate ideas back and forth between each other. This helped me recognize what each language is good for and understand the main goals behind their creation.

Recently, I started to come across a lot of Rust content on YouTube. It's a new, interesting language that has a different mentality and design. After watching and reading more about it, I decided that I should give it a shot.

Rust Resources that I liked

The official Rust website has great resources to help you learn the language. my favorite so far is mixing between The Rust book and Rust By Example. Both work best together.

The Rust book walks you through the basics of the language and explains every bit. It contains projects and snippets so you can try it yourself and make sure to exercise.

The Rust By Example, takes the "learn from code and comments" approach. It doesn't go deep as the Rust book, but it gives you a quick and easy resource to review and remember the syntax, which is quite useful.

Another Resource That I like, is the Let's Get Rusty Youtube Channel, It has a playlist going through the Rust Book, which is high quality and well presented!

Make no mistake, I'm not a Rust expert by any means, I'm still learning and going through the "Writing crappy Rust code" phase and fighting with the compiler to get it to accept my code.

The differences that you will experience between writing Rust and Python are not hard to notice. In fact, you can see it from the very first program you run, these are mine so far (add yours in the comment section)

Rust is a statically typed language

Everything needs to be decided at compile time; there is no room for guessing or making decisions during runtime. Which is different from how Python operates. Python is Dynamically typed, Things get decided in the Runtime (when your program is running). Using type hinting in Python doesn't change this fact, It's a good practice to use, of course, I use it all the time, but it doesn't change how Python operates and doesn't enforce any kind of checks.

Rust Compiler is a strict safeguard

I sometimes use "annoying" instead of "strict," but I think I'm in a phase of my career where I appreciate it to some extent. It doesn't allow you to write code that is not safe, or that can lead to any unexpected behavior. It's a safeguard for your future self that ensures the code you write now will still work as expected no matter what time in the future. Of course, that doesn't come without a cost; the main cost here that I see is the slow write/development pace. It's hard to get it right from the beginning, and it takes time to write in Rust, especially if you are just starting. You will see the huge difference in time between writing Rust and writing Python. But I guess it gets better over time, and the correctness and safety aspects can compensate for the long development time with minimal maintenance time in the future.

No garbage collecting and no manual memory management.

Rust Comes with new ideas about how to manage memory. It doesn't have a garbage collection like Python and Java, neither Manual memory management like C++. It introduces the concept of Borrowing and manages it with the Borrow Checker.

The Borrow Checker in Rust enforces strict rules on how references to data can be borrowed, preventing issues like data races and dangling pointers. By keeping track of which parts of the code have access to certain data, the borrow checker allows Rust to automatically manage memory, making your code safer and more efficient.

It doesn't trust you, it knows that you will mess things up at some point, which is annoying for your ego, but super helpful for your goal!

Enums are strong in Rust

I like the Enums in Rust and how it is empowered by Rust Type System. Enums in Rust let you create a type with different options, each holding different data. This makes your code shorter and clearer, helping you work with complex data and handle different situations smoothly. Enums also use Rust's strong type system and pattern matching, making your code safe and efficient.

Learning Rust made me write better Python Code

Yeah, I know it might seem silly, and annoying to my own ego. But Learning Rust made me conscious about things that I didn't use to spend too much time thinking about. Writing Python makes you productive in the development time, it's super flexible and forgiving. Python trusts you most of the time that you are going to do the right thing, which is sweet but harmful. Rust doesn't give you a chance to do anything that it doesn't like, which is safe but super annoying sometimes. Learning from the strictness and the seeking for correctness from Rust and mixing it with the flexibility and simplicity of Python makes me write better code that is safer.

Conclusion

Learning different languages with different designs open your eyes to new aspects and makes you improve. This happened to me when I learned other languages like Go, JS, TS, Java, and C as well as it's happening now while Learning Rust. I don't know if I'll end up proficient in Rust anytime soon (which is unlikely) or even use it in a big project. But I know that it has lots to offer, and the learning journey itself adds a lot to me.

I hope this is not the last time I write about it here, I still have lots to talk about!

Did you find this article valuable?

Support Mohamed Ashour by becoming a sponsor. Any amount is appreciated!