For those who have not yet dabbled in or even heard of Rust, its benefits are neatly captured in the slogan “Fast, Reliable, Productive – Pick Three”. Essentially, Rust is a modern programming language which offers unparalled compile-time safety coupled with runtime performance that easily matches or sometimes even exceeds that of C or C++.
C & C++ are the traditional languages for implementing high performance software, especially on embedded platforms. However, given the well-known complexities and security pitfalls of these languages, many large tech companies are looking at Rust as an alternative. The recent formation of the Rust foundation by these companies arguably shows just how committed they are becoming to Rust.
In response to this rising popularity, we at KDAB have explored different solutions that allow developers of existing Qt codebases to start working with this new technology.
We are not the first to identify the need for and attempt to form a marriage between Qt and Rust, but we believe that we are the first to come up with a practical approach that can be used to add Rust to existing C++ projects, today! Even though our approach works for both Qt Widgets and Qt Quick, it specifically targets Qt Quick as we feel there are more opportunities to optimise the experience in that regard and we also feel that Rust will see adoption in the embedded market, a traditional stronghold of Qt Quick, first.
There are previous approaches that try to wrap Qt in its entirety, as to form something that looks like a Rust library, but we are not aware of complete implementations of this. We also believe such an approach is unlikely to ever really work out given the fundamental differences between Qt and Rust which make using Qt from idiomatic Rust problematic.
Given that Rust code can be linked directly into the same binary as C++ code, we feel that a far more practical, safe and maintainble approach is to have hybrid C++ and Rust code bases with tools that make crossing the language boundary as safe and efficient as possible. The CXX library strives to offer exactly this, but as it doesn’t have native CMake support (yet?), it is difficult to use with Qt projects. Our solution to this is called CXX-Qt.
CXX-Qt not only offers an integration of CXX with CMake, but also offers convenience macros that make it possible to automatically generate QObject derived classes directly from Rust structs backed by Rust logic. These objects can then be directly exposed into QML thereby even eliminating the need to write any C++ for simple cases. In the future, we plan on adding additional macros to generate code for other common use cases, but in the meantime, you can use “raw” CXX macros to bridge anything you desire from Rust in C++ manually and perhaps even write your own helper macros.
About the speakers
Andrew Hayzen is a Qt developer who works with QML, Rust, and Linux systems at KDAB. He has experience with all aspects of developing QML applications and has a keen interest in improving developer workflows. Andrew holds a BSc in Software Engineering.
Leon Matthes is a working student and software engineer at KDAB. He has experience in C++, Rust and Linux with a focus on embedded programming and software architecture. Leon is currently pursuing a M.Sc. in IT-Systems Engineering.