Rust or C++, I am not sure yet

About 40 years ago I started with a TRS 80, BASIC, Z80 assembly, later I learned C, x86-assembler, Pascal, SQL, and then I got a job and mostly forgot about programming.

Recently I picked up on C again, and I feel like it is time to learn something new. I always had some irrational fear of C++, but I asked around and was really getting ready to dive in.

Then someone had a compelling story about Rust. And one very good reason to learn Rust he gave me: there is this place where people are very kind and helpful, even for amateurs like you (that would be me).

So there I am. No questions yet. But I do think I am going to try to learn. Wish me luck :innocent:

17 Likes

Good luck!

You should mostly have an easier job than people coming to Rust from a higher level language, most of the time you will be trying to find the safe equivalent to what you did manually before. Box/Vec/Rc for what you would malloc, etc.

A few basic suggestions:

  • Errors will be a lot more helpful than you're used to, try to avoid glossing over the message!
  • The library system, cargo, is a lot more simple to use too, once you've got your feet under you try to remember to look for a crate (lib.rs is a bit nicer than crates.io). I've suggested a few good "default" crates below that most projects can use.
  • Use the automatically installed rustfmt (format) and clippy (linter): in your editor if you can easily, but they are also available with cargo fmt and cargo clippy.
  • Avoid using too complex lifetimes, especially. If you're looking at putting lifetimes on a type, you've most likely gone too far. In most cases you can just share with Rc or use indices instead of references instead.
  • Coming from C especially, don't think of unsafe as an "escape hatch": Rust has more aggressive guarantees for the complier/optimizer than you're used to, and unsafe expects you to uphold them. You should generally expect not to need it, but refer to the rustonomicon and use MIRI to verify if you do want to give it a try.

Some good crates to start with are:

  • clap for argument parsing
  • serde for file formats (eg JSON)
  • thiserror for easy error types
  • tracing for logging/instrumentation.
17 Likes

Thank you!

JSON was one of the things I struggled with, so you make me happy already :slightly_smiling_face:

If you have never written C++ and don't have a huge legacy project to maintain, then there's little reason to start writing your new projects in C++.

Knowledge of C++ can come in handy for interacting with existing C++ code and for learning concepts that didn't necessarily come up in C (eg. iterators). However, for serious green-field projects, I think Rust is unambiguously the better option, due to the safety aspect and the significantly better, unified, modern tooling.

13 Likes

I have maintained a bit of fear for C++ for decades :grin:

2 Likes

Rust can run faster than C, while Rust without safety checks. So I suspect that C with safety checks may be slower than Rust.

2 Likes

As always... "It depends". As a simple example, Rust requires that you mutate shared objects through a Cell or Mutex type, so you have to drop down to unsafe to really squeeze out every last cycle if you know that it's not needed - but such cases are quite rare.

Having spent three almost sleepless days in the windowless offices of a disreputable energy company debugging an off-by-one error I can say that your fear is not irrational.

16 Likes

I've written a lot of C++. Ten years ago, it was the best language for many things, where you want control and performance, and I would have heartily recommended learning it.

However, it is a complex language. And really hard to write reliable and safe code in C++. You have to keep so much in your head, and each project or team will have different conventions that you have to learn.

On the other hand, in Rust, the language and the compiler take care of you. The right way to do things is the easy way. You don't have to constantly worry about whether you've correctly implemented a copy constructor or whether you have done 100% of necessary null pointer checks.

Also, in Rust it is SO MUCH EASIER to use external libraries. For instance, an HTTP server, or JSON parsing, etc. whatever you want is just a "cargo add" away - and it has a built-in build system that is portable to all platforms.... I could go on and on.

As someone who's done C++ using many tools, from Microsoft Visual C++ (ugh) to GCC using Makefiles, CMake, and other build systems (which are all much more primitive than Rust's Cargo, and hard to make portable to Mac and Windows and Linux), the simple, unified, powerful build tool Cargo is alone a reason to use Rust.

But if you are a masochist and you enjoy pain, feel free to write AND MAINTAIN software written in C++ :slight_smile:

Or experience the beauty of Rust.

8 Likes

Also, as you said you've learned some C, that is great. I think all programmers should still learn C. It is a simple language that teaches you how the machine works. It doesn't hold your hand, which is good, because you learn what can (and will) go wrong when mistakes are made.

After learning C, the design of Rust will make more sense. If you've tried to any serious software in C, you'll have figured out that memory management is hard, and you'll be better able to understand why the Rust borrow checker and ownership model is so amazing.

2 Likes

Convincing arguments I must say :slight_smile:

I am a bit of a masochist when it comes to programming, but I also like to try something not 90% of the world is using. Time to find out how to get Rust on my Pi I think

I am a bit of a masochist when it comes to programming, but...

Oh, I think you misunderstood: Rust may be in some respects easier than C++, but it is by no means an easy language to master. :smile: So, I guess the masochist in you will be very pleased with Rust. :wink:

6 Likes

Rust installs on a Pi easily with the instructions for MacOS and Linux you can find here: Install Rust - Rust Programming Language

One can also install Rust onto PiOS from deb packages. I have never done that. In the past with other languages I found the Debian packages for the language and libraries were always somewhat out of date. So I use rustup and cargo on the Pi to manage Rust.

3 Likes

That makes me happy :smiley:

So I installed Rust, and it works. I noticed rustc is a lot slower than gcc, but I am told that is to be expected because Rust is so much stricter than C.

I made a bash-script before, to edit my C with nano, and a standard template file, made a copy for use with Rust. So ready to go (and tried a first simple program, that worked :slight_smile: )

With C I use a simple Makefile, so I can always compile my (simple!) programs with

make src=source.c exe=executable

Do I understand correctly that I should use “cargo” with Rust where I use “make” with C?

Yes, you don't need any script boilerplate or handwritten makefiles, since Rust has a real build system.

Using cargo instead of invoking rustc directly also helps with dependency management and incremental compilation (which makes subsequent invocations a lot faster than always recompiling every file with rustc).

6 Likes

Yes, use cargo. The basics are quick to learn.

  • Create the project with it
    • cargo new $NAME
    • cargo new --lib $NAME
  • Check / build / run with it
    • cargo check
    • cargo build
    • cargo run
    • cargo check --release
    • cargo build --release
    • cargo run --release
  • Add dependencies with it
    • cargo add $NAME
6 Likes

This is sort of true: Rust is simply a much more complex language than C that often needs to look at a lot more data - but it's quite possible that the essential difference there is pretty negligible. Really it's that Rustc is just a much younger compiler that's had a huge amount of functionality added in over that short lifespan with relatively little attention paid to compile times. That is definitely becoming less true now, but it's going to take a long time, if ever, before Rust is really snappy to build.

It will probably always be slower so long as it's doing strictly more work (trait solving, monomorphizing, borrow checking). But I agree that being young and still evolving at a relatively fast clip plays a big role compared to being more stable and having decades of development history (so the gap will probably shrink over the years).

1 Like

So I have installed Rust on my Raspberry Pi, wrote a bash-script + cronjob to update regularly, and tried my first Hello World-like program, using cargo.

Not much to be enthusiastic about yet maybe, but well, cargo is just that bit nicer than what I am used to, that I like it already. First baby step, this baby is happy :baby:

3 Likes