C++ for Rust Developers
Transfer Rust experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.
Rust and C++ side by side
The same program in Rust and in C++, from the lessons of this course.
Strings and Output
Rust separates owned String from borrowed str and enforces UTF-8. C++ string has no automatic UTF-8 validity guarantee. C++ std::string owns a mutable byte sequence; stream insertion formats values. Length counts bytes, not Unicode characters.
Rust1 2fn main(){3println!("{}", "oak".len());4}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}Text Equality
Rust PartialEq expresses equality across declared types. C++ string supplies equality, while raw pointers still compare addresses. C++ std::string equality compares contents. Raw pointer equality compares addresses; user classes need a deliberate equality…
Rust1fn main() {2let a = vec![String::from("red")];3let b = a.clone();4println!("{}", a == b);5println!("{}", std::ptr::eq(&a, &b));6}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}Fixed-size Arrays
Rust arrays also include length in their type. C++ array access with [] is not automatically bounds-checked. C++ std::array<T,N> is a fixed-size value type. Its size is part of its type, and normal copy duplicates its elements.
Rust1 2fn main(){3let a=[4,5,6];println!("{} {}",a.len(),a[1]);4}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}Value Copies
Rust moves non-Copy values by default and uses clone explicitly. C++ ordinary copy initialization usually invokes copying instead. C++ vector and string copies own independent storage. Copying a raw pointer still shares the pointed-to object and does not…
Rust1fn main() {2let original = vec![vec![1]];3let mut copied = original.clone();4copied[0].push(2);5println!("{:?}", original);6println!("{:?}", copied);7}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15}Functions and Returns
Rust functions declare return types and can return a final expression. C++ requires an explicit return statement for returned values. C++ function declarations establish parameter and return types. A return value is not automatically printed.
Rust1fn main() {2fn twice(value: i32) -> i32 { value * 2 }3println!("{}", twice(4));4}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}Streams and Exact Parsing
Rust parsing returns Result values; C++ stream extraction uses state instead. C++ stream extraction reports success through stream state. Validate conversion and reject unexpected trailing tokens when the contract requires exact input.
Rust1 2fn main(){3let n="7".parse::<i32>().unwrap();println!("{}",n);4}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsC++ Files and Values
6 lessonsConditions and Iteration
5 lessonsStandard Containers
5 lessonsCopies and Algorithms
5 lessonsFunctions and Templates
5 lessonsParsing and Lifetime
5 lessonsObjects and Invariants
6 lessonsComposition and Ownership
5 lessonsProject: Library Desk
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is C++ for Rust Developers for?
For people who already write Rust and want to learn C++. Each lesson starts from Rust you know and shows how C++ does the same thing, so you skip the beginner basics.
Do I need to know C++ already?
No. The course starts with your first C++ program. You need Rust basics: variables, conditions, loops, functions and collections.
How many lessons does C++ for Rust Developers have?
C++ for Rust Developers contains 48 interactive lessons you can complete at your own pace.
What does C++ for Rust Developers cover?
The course is organized into chapters covering: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, and more.
Is C++ for Rust Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.