Rust for C++ Developers
Bring C++17 experience with RAII, references, templates, and virtual interfaces to Rust ownership, borrowing, enums, and traits. Solve 48 multi-file tasks, build a parcel depot, and port two C++ programs.
C++ and Rust side by side
The same program in C++ and in Rust, from the lessons of this course.
Output and Comments
C++ stream insertion sends each operand to an output stream. Formatting can persist on that stream: std::boolalpha changes later Boolean insertions. Rust's println! is a macro with a format string, and ordinary Boolean formatting already uses words. It…
C++1#include <iostream>2int main() {3 // std::cout << "hidden\n";4 std::cout << std::boolalpha << true << '\n';5 std::cout << "// data\n";6}Rust1fn main() {2 // println!("ignored");3 println!("port={}", 7);4 println!("// data");5}Boolean Conditions
C++ permits contextual conversion to Boolean in conditions, so a nonzero integer or a nonnull pointer can be used directly. Rust conditions require bool; compare a count with zero or inspect an optional value explicitly. Both languages short-circuit built-in…
C++1#include <iostream>2int main() {3 int pending = 2;4 bool available = pending && true;5 std::cout << std::boolalpha << available << ' ' << (pending == 0) << '\n';6}Rust1fn main() {2 let divisor: i32 = 0;3 let usable = divisor != 0 && 12 / divisor > 2;4 println!("{} {}", usable, !usable);5}Functions and Returns
C++ function declarations put the return type before the name and can separate a declaration from its definition. Rust function definitions put parameter types after names and the return type after an arrow. A final expression without a semicolon supplies the…
C++1#include <iostream>2const char* orientation(int dx) {3 if (dx == 0) return "still";4 return dx > 0 ? "east" : "west";5}6int main() { std::cout << orientation(0) << ' ' << orientation(-2) << '\n'; }Rust1fn label(value: i32) -> &'static str {2 if value == 0 { return "idle"; }3 if value % 2 == 0 { "even" } else { "odd" }4}5fn main() {6 println!("{} {}", label(0), label(7));7}Ownership and Moves
C++ std::move is a cast that enables move overload resolution; it does not itself transfer a resource. A moved-from standard-library string remains valid but has unspecified contents unless a stronger guarantee applies. Rust moving a String transfers…
C++1#include <iostream>2#include <string>3#include <utility>4int main() {5 std::string original = "draft";6 std::string duplicate = original;7 std::string owner = std::move(original);8 original = "reset";9 owner[0] = 'D';10 std::cout << owner << ' ' << duplicate << ' ' << original << '\n';11}Rust1fn main() {2 let first = String::from("note");3 let copy = first.clone();4 let mut owner = first;5 owner.replace_range(0..1, "N");6 let a = 3;7 let b = a;8 println!("{} {} {} {}", owner, copy, a, b);9 drop(owner);10}Strings and UTF-8
C++ std::string stores bytes and does not require UTF-8 validity. Its indexes refer to code units, and std::string_view can designate an arbitrary byte range. Rust String and str guarantee valid UTF-8; byte indexes cannot split a multibyte scalar when…
C++1#include <iostream>2#include <string>3int main() {4 std::string word = "\xC3\xA9x";5 word.push_back('!');6 std::cout << word.substr(2) << '\n';7}Rust1fn main() {2 let mut word = String::from("éx");3 let invalid = word.get(1..).is_none();4 word.push('!');5 let suffix = word.get(2..).unwrap();6 println!("{} {}", if invalid { "invalid" } else { "valid" }, suffix);7}Option and Matching
C++17 std::optional<T> distinguishes an engaged value from absence without reserving a sentinel in the value domain. Rust Option<T> has the variants Some(T) and None and serves the same modeling purpose. Some(0) is present, just as an engaged C++ optional…
C++1#include <iostream>2#include <optional>3int main() {4 std::optional<int> slot = 0;5 if (slot) std::cout << "present:" << *slot << '\n';6 else std::cout << "missing\n";7}Rust1fn main() {2 let values = [0, 5];3 match values.iter().position(|&n| n == 0) {4 Some(index) => println!("found:{}", index),5 None => println!("missing"),6 }7}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Rust Programs
6 lessonsDecisions and Loops
5 lessonsFunctions and Sequences
4 lessonsOwnership and Borrowing
5 lessonsText and Collections
4 lessonsAbsence and Failure
5 lessonsStructs and State
6 lessonsTraits and Generic Code
7 lessonsProject: Parcel Depot
4 lessonsFinal Ports
2 lessonsFrequently asked questions
Who is Rust for C++ Developers for?
For people who already write C++ and want to learn Rust. Each lesson starts from C++ you know and shows how Rust does the same thing, so you skip the beginner basics.
Do I need to know Rust already?
No. The course starts with your first Rust program. You need C++ basics: variables, conditions, loops, functions and collections.
How many lessons does Rust for C++ Developers have?
Rust for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does Rust for C++ Developers cover?
The course is organized into chapters covering: First Rust Programs, Decisions and Loops, Functions and Sequences, Ownership and Borrowing, and more.
Is Rust for C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.