Rust and C++ side by side

These pages are for programmers who write C++ and are learning Rust. Each one takes a single task, writes it in both languages, builds both programs with the real compilers, and shows the two outputs next to each other. Then it explains what differs and why: who owns the value, what the compiler rejects, and what happens at run time.

The interesting part is rarely the syntax. It is the habit that compiles in C++ and stops at rustc, and every page ends with exactly that mistake and the diagnostic it produces. The pages are independent of the course: read one when its topic comes up in your work, and follow the links into the Rust course and the C++ course for the full lesson.

Mut Variables and mutability let and mut against int and const, shadowing against a block, and a narrowing that C++ accepts and Rust refuses. Open Fn Functions, overloading and default arguments Two functions of one name and a defaulted parameter in C++, and the separate names and builders Rust puts in their place. Open Move Ownership and moves A moved-from std::string that stays usable in C++, and the Rust binding the compiler refuses to read. Open Drop RAII and Drop A guard that closes on an early return in both languages, copied once in C++ and moved once in Rust. Open Ref References and borrowing A const reference and a mutable reference in one function, and the dangling reference that only one compiler refuses. Open Struct Structs and classes A class with private members against a struct and an impl block, and the field that C++ fills with zero. Open Const const and mut A getter with a side effect that compiles in C++, and the same method that rustc stops with E0594. Open Str Strings Append, find and cut a string in both languages, and the character assignment that only C++ accepts. Open View String views and slices string_view against &str, and the view of a local that compiles in C++ and is refused with E0515. Open Union Enums and variants std::variant against an enum with data, and the wrong alternative that throws in C++ and does not compile in Rust. Open None Null and Option A lookup with std::optional and with Option, and the empty optional that C++ dereferences and Rust refuses. Open Box Smart pointers unique_ptr and shared_ptr against Box, Rc and Arc, the same counts, and the copy that Rust moves. Open Err Errors and exceptions std::stoi with a try block against Result and a match, and the uncaught exception that terminates the C++ process. Open Trait Inheritance and traits A virtual function against a trait object, and the by-value base parameter that slices the object in C++. Open Gen Templates and generics One function for two types in both languages, and the unconstrained generic that Rust rejects at the definition. Open Op Operator overloading operator+ and operator<< against Add and Display, and the integer that only the C++ constructor converts. Open Macro Macros and templates A variadic template against macro_rules!, and the call with two values that only the template accepts. Open Iter Iterators and algorithms begin and end against a Rust iterator, and the push_back during a walk that invalidates the C++ loop. Open Thread Threads and data races A thread that writes into a captured variable in C++, a thread that returns its value in Rust, and E0373. Open Make Build tools A Makefile against Cargo.toml, the commands each one prints, and the new file only one build file needs. Open Test Testing A CHECK macro and a hand-written runner against cargo test, and the assert that vanishes under NDEBUG. Open Size Memory layout sizeof and padding in both languages, the order Rust is free to change, and repr(C) that matches C++. Open UB Undefined behaviour A pointer to a dead local in both languages, the warning g++, the refusal of rustc, and the unsafe block that compiles. Open Cexpr constexpr and const fn A table the compiler builds in both languages, the assertions that check it, and the constexpr std::string that only C++ accepts. Open