Outils pour utilisateurs

Outils du site


issue150:c_c

Ceci est une ancienne révision du document !


Every year I make a point to learn a new programming language or re-visit one I haven’t used for years. A couple of years ago, the new language I wanted to play around with was Rust. I followed a few tutorials, got comfortable with the syntax, did a test project, and then…stopped. Between my job and my comfort in Python, I just didn’t have a lot of situations where I felt like I wanted to take the time to apply a new language, and defaulted to Python instead. As my day job currently consists of Python (and a few other languages), I have found myself revisiting Rust simply as a way for my personal projects to not feel quite so much like work. As such, I’d like to give my readers my take on Rust.

What is Rust?

Rust is a systems language that aims to be safer and more stable than C++. It does this by checking for possible errors at compile time, and giving errors/warnings to the developer at that point in time. The error-checking is extremely powerful, and cargo (the build tool) offers enough information to almost always solve the problem directly from there.

Why use Rust?

If you’re coming from an interpreted language like Java or Python, here are a few things that are different for Rust (or any systems language, really): • Speed - as Rust is a lower-level language, you can typically assume it will perform faster (once compiled) than an interpreted language, and with less memory overhead. • If you build a Rust binary (without external dependencies like OpenSSL) it will depend only on system libraries. Meaning running a binary will not require Rust to be installed on the target system. While interpreted language can emulate this effect, they are actually being compiled with a portable version of the interpreter. • Errors on compilation - in both Java and Python, the interpreter will catch some critical errors. However, you can still run into runtime issues where the application crashes. Rust aims to catch these at compilation time and force the developer to fix them. • Lastly, just like C in Python, you could theoretically use Rust with Python (admittedly, I’ve never tried this). But if you need to optimize some code and know Python just won’t get the speed you need, you can write a library in Rust and use that.

If you’re coming from another systems language like C++, here is my best understanding of what’s different (based on my admittedly limited C++ knowledge): • Rust is memory/type safe (by default - you can integrate unsafe practices if required). Rust will catch most errors at bu• ild time. • Rust offers a build tool and package manager called cargo. • A massive library of easily-accessible 3rd party libraries via cargo. • Performance-wise, it typically is a bit slower than pure C, but a bit faster than C++.

Resources

Books: “The Rust Programming Language” - the official rust book - https://doc.rust-lang.org/book/title-page.html “A Gentle Introduction to Rust” - https://stevedonovan.github.io/rust-gentle-intro/

Videos: The official Rust YouTube channel: https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA

Practice: Exercism has a track for Rust: https://exercism.io/tracks/rust

My Opinion of Rust

My main language of choice has been Python for quite a long time (outside of the web), and I’ve always meant to sink some time into a systems language, but never really enjoyed C++. So Rust seems like an ideal choice – modern language, similar C++ power. Cargo, especially, makes Rust feel a lot more modern.

That being said – what if you don’t know any other programming languages? Or what if you’re not sure you want to learn a systems language? Here are a few points as to why I think Rust could be a good language to learn on: • Type and memory safe - learning something like C++ can be frustrating because a mistake can mean a segmentation fault, and debugging them can drive you crazy. Rust will do its best to prevent these from ever happening. • Cargo - when you compile Rust, you get formatted output telling you what line (not just the line number, but the actual code too), with markup and annotations telling you what issue the compiler sees. It’s essentially guiding you through learning the code. It may feel disheartening at first to need multiple passes to fix your code, but eventually you’ll actually understand the issues. • Computer science - if you’re interested in the topic at a deeper level, Rust is a much better choice than Python for a simple reason – data types. Python will just select the right type based on the value given. Rust can do the same to some degree, but you’ll certainly want to learn the difference between signed and unsigned ints, and select the correct type for a situation (i.e. an 8-bit unsigned int instead of a 32-bit int). This knowledge can make you a much better programmer long-term, because you’ll understand memory usage at a deeper level. • Community - the entire Rust community is founded on the idea of guiding people through the code, and as such it is one of the most open and welcoming communities I know of. If you’re someone who prefers to be able to ask a person questions when learning, you’ll typically always find someone to answer your question in this community (i.e. on the Rust subreddit).

Ready for Prime Time?

If you’re a professional programmer and you want to start using Rust at work, you may be asking yourself if it’s ready for that. Here are a few of my thoughts on the matter: • From an architecture perspective it’s more ready than C++ ever was • If you have a specific tool or library you need to use with your software, make sure you can integrate it with Rust (i.e. check crates.io for libraries). • While the very first version of Rust was released in 2012, it hit version 1.0 only in 2015. As such, it’s hard to say whether it’s here to stay. I’m of the opinion that the approach Rust has is the way we need to go for the future, and, being an open-source project, should continue to be developed as long as people are using it. Depending on how long-term your profession needs to think, you’ll need to make a decision yourself on how mature the language is. • If performance is a big factor, make sure you want to invest the time into learning how to optimize Rust. • If you’re in a position where you have a codebase where you can mix and match (i.e. Python with C libraries), you could implement one small library in Rust the next time you need a new one and try it out that way.

The tl;dr is: It depends on your specific scenario. In that position, you’ll need to know enough to decide for yourself.

Conclusion

I sincerely hope that Rust continues to grow in popularity, as its methodology is a big step forward (in my opinion). For any of my readers who know Rust, or learn it - let me know how it goes! Similarly, if anyone has any requests for an article, questions, or just wants to share their opinion, you can reach me at lswest34+fcm@gmail.com.

issue150/c_c.1572506715.txt.gz · Dernière modification : 2019/10/31 08:25 de auntiee