loading...

Why I moved from C++ to Rust

pyxel profile image Brayden ・3 min read

As people know C++ has been around for years, decades even, however despite the language being as mature as it is, the language is still getting new features, which depending on how you look at it, can either be a good or bad thing. In this article/post I'm going to lay out the foundation of why I personally am moving to Rust.

Library / Package Management

Let's be honest here, C++ despite being as mature as it is, has horrific package management, having to manually download each library your package needs, adding it to your project via MAKE or a MAKEFILE generator such as CMake, or Premake, while generators do make your life easier in a lot of cases, it's still quite a hassle.

In rust on the other hand, it's as simple as either dropping a line into your Cargo.toml file or by executing cargo add <library>, and this compared to C++ is a honest luxury when it comes to a casual development workflow, especially for those on a time crunch.

Thread Safety

One thing rust does amazingly well, is enforce thread safety, while similar to C++ in the fact it does not use any type of garbage collector or memory management, it does enforce you to use thread and data safety, so that data shared across threads among other things is done in such a way data corruption and race conditions don't happen, at least not as often as they do in the C family of languages.

Error Management

This section may or may not be a somewhat controversial topic, as some people love it, and some people hate it, personally I love how Rust manages it's error handling. While it can become somewhere tiring to write out error enums for a lot of things, it does force you to know what type of errors are possible in your code. If you write an error enum and it's missing a possible error type that a function could throw, it's going to yell at you saying "hey, this can also throw this error!" which when it comes to error management, is a honestly nice feature, whereas in C++, errors can just be.. thrown without having to specify what exactly types of errors, it should be able to.

Syntax

Alright, I'll admit in some cases Rust's syntax is quite odd, Ok(()) being an example of that weird syntax. But in a general sense, Rust's syntax is in my opinion a lot more modern than any of the C family of languages (except for maybe C#, but it still has the general style that the rest of the C family does). However despite the few things I find odd about the language's syntax, it does have a lot of thing I honestly love, such as return types being after the function declartion instead of before, as well as expressions such as if let. It's also quite cleaner in my opinion than C has ever looked, as an example:

fn main() {
    println!("Hello World");
}

In my honest opinion looks a lot cleaner than how you would do this normally in C++:

int main()
{
    std::cout << "Hello World" << std::endl;
}

Conclusion

There's many, many more reasons I have decided to switch from using C++ to Rust almost if not exclusively, but that is a few reasons or a general overview of why I have, Rust is a lot different than the C family of languages in many ways, and as such it takes a while to get used to, especially as it enforces a lot of things at compile time (including thread safety). In the end, it's a matter of which one best suits the project at hand, but in this case, with both languages being somewhat system languages, I think Rust will certainly take the cake in most of my projects that require one.

Posted on Mar 14 by:

pyxel profile

Brayden

@pyxel

A hobbyist and freelance developer, designer, and gamer that likes to code random useless utilities, programs, etc.

Discussion

markdown guide
 

I really want to play with Rust more, but the lack of any UI frameworks has been a bit of a disappointment to me. I would love to see a better way to create GUIs without ugly C++ code, but I was unable to find any projects with major momentum :(

 

I would love to see a better way to create GUIs without ugly C++ code

Qt is fantastic, if you wanna give it a whirl

 

There is plenty of C++ UI bindings in rust :) although there isn't many decent ones that wrap them in a elegant manner.

 

I've got a few languages in my toolbox, but no system-level language. I can deal with C if I have to, but it's just a major pain for so many reasons. While I do think that rust has nice concepts (and I think that "letting the compiler do as much of the work as possible" is generally a good idea), the syntax is really messed up, and I mean REALLY messed up as in single-quotes not being used in pairs. So my search for a system-level language that I actually enjoy working with continues. Things have improved over the last years though.

 

I personally like how Rust's syntax is, it's definitely different.

 

Nice article 🙂 I'm also really enjoying the simplicity of the Rust package management. It makes a difference when the language is designed to specifically solve common developer pains. It's a joy to use!

 

Absolutely! It was such a relief to use when I started using it.

 

Thanks! You have one new follower.