With C++

The CXX crate makes it possible to do safe interoperability between Rust and C++.

The overall approach looks like this:

See the CXX tutorial for an full example of using this.

  • At this point, the instructor should switch to the CXX tutorial.

  • Walk the students through the tutorial step by step.

  • Highlight how CXX presents a clean interface without unsafe code in both languages.

  • Show the correspondence between Rust and C++ types:

    • Explain how a Rust String cannot map to a C++ std::string (the latter does not uphold the UTF-8 invariant). Show that despite being different types, rust::String in C++ can be easily constructed from a C++ std::string, making it very ergonomic to use.

    • Explain that a Rust function returning Result<T, E> becomes a function which throws a E exception in C++ (and vice versa).