Switching from C++ to Rust: A Significant Improvement in Work Experience!

Follow us丨Book giveaway at the end of the article
I have been working professionally in C++ programming for over four years, but three months ago I found a new job that requires using Rust. I want to share my personal experiences and thoughts on switching between the two languages in this article.

Disclaimer: This article does not intend to compare C++ and Rust, and the views expressed are based on personal experiences.

01

Introduction to C++ and Rust Work

I believe that the nature of the work one does greatly affects the experience with the language, so let me briefly introduce the background.
My work with C++ has mostly involved writing databases. Databases are not common applications; they typically monopolize servers, can make extensive system calls, and in some cases, can even bypass the kernel entirely. On the other hand, this means that databases are an interesting sample for studying specific languages, as you need to focus on performance, provide a good user experience for clients, and ensure correctness.
Due to the company’s confidentiality agreement, I cannot share the details of the work. My environment for running Rust is a high-load, high-performance digital computation asynchronous server. It’s somewhat similar to a database, but the user experience may not be as critical.

For the purposes of this article, I believe these two are quite similar.

02

Frequently Discussed Aspects

Rust provides good memory safety guarantees.This topic has been discussed repeatedly, so I will not elaborate here. Although I have used C++ for over four years, I occasionally encounter memory issues that cause server crashes, even in code that has been reviewed and merged. It’s hard to say how much of this code has made it into production because people just restart the server when segmentation faults occur. Fuzz testing can ensure coverage of some strange cases, but it is ultimately not a panacea. Overall, I feel more at ease delivering Rust code than C++ code.

03

Build Systems

In my daily work, the most annoying task is building C++ code. As a developer, I want to be able to write a short command to build the entire project. “Before building on the server, you just need to run these two commands…” This phrase is terrifying because it implies that the build process is multi-step. Do I have to run these two commands every time? What does it mean that these two commands only need to be run when these two files change? How would these commands change if I used sanitizers to build the project? What does it mean that the build process does not support sanitizers? Why does the build script suddenly start outputting link errors?
I have said all this to express one point: the lack of a unified build system is truly frustrating.Bazel is a step in the right direction. CMake is still my nightmare.

After experiencing all this, I feel like Rust has pulled me from hell to heaven. You only need to write a short command to build the entire Rust project. More importantly, all other Rust projects in the world use the same build system, so you don’t have to convert build scripts from their system to your own. You just need one line in Cargo.toml to include all dependencies in the build process. It even automatically passes the correct compile flags.

04

Compiler

The error messages from two compilers can be really frustrating; you need to put in a lot of effort to understand and fix them correctly.
In C++, the size of error messages is measured in kilobytes. Infinite scrolling in the terminal emulator is absolutely necessary because C++ compilers love to output text. After years, you develop a sort of intuition to judge whether you should read the error or look at the code just by seeing how many errors there are. Usually, the larger the error message, the more likely it is that skimming the code will be more beneficial than reading the error message. I feel that if C++ does not change the way templates are defined, this problem will never be solved.

In Rust, compiler errors (after fixing all spelling mistakes) are often very poor messages. Typically, these error messages indicate that you need to reorganize your code in some way or spend some time adjusting lifetimes so that you do not misuse memory. While this process takes time and can be annoying, the right approach is to seriously listen to the compiler’s suggestions. It’s embarrassing to say, but taking suggestions often helps to write better code. Moreover, error messages can usually be displayed fully on one page, making them convenient to read.

05

Type System

Expressing ideas in Rust’s type system is a joy.
First of all, I am very grateful for the absence of duck typing in generics.Traits clearly express the contract structure or functions that types expect to see. This also helps the compiler generate useful error messages. The error messages we receive are no longer: “Invalid reference to method clone() on line Y,” but rather, “Type X does not implement Clone,” which is clear and straightforward.

Secondly, enums are very powerful.Result and Option are very powerful concepts that everyone uses. These two enums are the common language for expressing error-prone computations and optional values across all libraries (including the standard library). In C++, we have (1) return error codes; (2) return invalid values; (3) throw exceptions; (4) crash the process. All problematic options use different options, and every library has its own choices. Besides Result and Option, I find the ability to define tagged enums very convenient as well.

06

Conclusion

Overall, after switching to Rust, I feel that my daily work experience has improved significantly.Rust’s tools are very friendly, the language is expressive and powerful. I really like Rust, and I hope its future development can avoid some of the pitfalls of C++.
07

Recommended Books

1

C++ Primer Plus 6th Edition Chinese Version

Switching from C++ to Rust: A Significant Improvement in Work Experience!

Click the cover to purchase the book

Another classic introductory work by the CPP author, best-selling for over 20 years.
This book uses a large number of short and concise programs to clearly explain the basic concepts and techniques of C++, making it the best choice for beginners in C++. It is designed for readers with no background, includes many code examples, and provides clear warnings for common pitfalls, fully caring for beginners.
C++ performs excellently in large application development and can be seen as a supplement to C, but simpler. If you have a foundation in C, learning C++ will be easier.

2

C++ Primer Plus 6th Edition Chinese Version Exercise Answers

Switching from C++ to Rust: A Significant Improvement in Work Experience!
Click the cover to purchase the book, limited time discount
50% off
The “C++ Primer Plus (6th Edition) Chinese Version Exercise Answers” is divided into 18 chapters,each chapter’s theme is completely consistent with “C++ Primer Plus (6th Edition) Chinese Version”.
Each chapter begins with a mind map listing the knowledge points for that chapter, then summarizes the key content of the chapter, and finally analyzes the review questions and programming exercises in each chapter and provides answer ideas, ensuring that readers solidify their theoretical knowledge while further enhancing their practical programming skills.

3

Qt 6 C++ Development Guide

Switching from C++ to Rust: A Significant Improvement in Work Experience!
Click the cover to purchase the book, limited time discount
50% off
The QT 6 C++ Development Guide, developed jointly by university teachers and senior engineers, covers both principles and applications!
QT has been deeply involved in cross-platform development for over 30 years, and this book systematically introduces how to develop applications using Qt C++ based on Qt 6.
This book introduces the basic structure of Qt C++ applications, visual design and layout management methods, the functionality and applications of the meta-object system, and then delves into programming methods for commonly used interface components, model/view structures, event handling, dialog and multi-window program design, file reading and writing, etc., as well as the use of modules such as databases, multithreading, networking, multimedia, charts, data visualization, and serial communication.
With a wealth of carefully designed example programs, readers can easily master various technical methods related to GUI program design.

4

Rust in Action

Switching from C++ to Rust: A Significant Improvement in Work Experience!
Click the cover to purchase the book
The content gradually deepens, introducing the Rust programming language through exploring various systems programming concepts and techniques, recommended for those interested in Rust programming!
This book introduces the Rust programming language through exploring various systems programming concepts and techniques, helping readers understand concepts such as Rust’s ownership system, Traits, package management, error handling, conditional compilation, and more, while exploring real-world examples to help readers understand memory models, file operations, multithreading, networking programming, and more in Rust.

5

C++ Concurrency in Action (2nd Edition)

Switching from C++ to Rust: A Significant Improvement in Work Experience!
Click the cover to purchase the book
A deep guide to C++ concurrency programming, written by members of the C++ standards committee, covering multiple aspects of C++ concurrency programming, with code accompanied by concise Chinese comments for easy understanding, and accompanying code files provided.
This is a deep guide to C++ concurrency and multithreading programming, covering multiple aspects of C++ concurrency programming, including starting new threads and designing fully functional multithreading algorithms and data structures.
The translation of this book has been carefully considered, with the translator participating in the translation and review of the entire content, and the code is accompanied by detailed Chinese comments, making it concise and easy to understand. The translator has also supplemented many extended knowledge points based on their own development experience, making it suitable for developers who want to gain a deeper understanding of C++ multithreading.
This book provides powerful accompanying resources, including nearly 200 pages of electronic version Appendix D and over 140 accompanying code files.
This article is reprinted from CSDN Linkhttps://mp.weixin.qq.com/s/ZXxRsxA8K6oJ71TPhbaRmA

—END—

Switching from C++ to Rust: A Significant Improvement in Work Experience!

What are your thoughts on Rust?

Participate in the interaction in the comment area, and click to view and share the event to your Moments. We will select one reader to receive a free book, deadline April 30.

Leave a Comment