Rust for PHP Developers
Build on PHP arrays, object references, exceptions and interfaces to learn Rust ownership, borrowing, enums and traits. Complete 48 multi-file tasks, build a parcel depot and port two PHP programs.
PHP and Rust side by side
The same program in PHP and in Rust, from the lessons of this course.
Output and Comments
PHP executes statements in a script after its opening tag, while a Rust executable starts at fn main. Both support slash comments; PHP also accepts hash comments. A comment marker inside quoted text remains data. PHP echo does not automatically append a…
PHP1<?php2declare(strict_types=1);3// This line is ignored.4echo "level:";5printf("%d # literal\n", 2 + 3);Rust1fn main() {2 // This line is ignored.3 print!("level:");4 println!("{} # literal", 2 + 3);5}Boolean Conditions
PHP boolean conversion treats an empty array as false, so the shorthand ternary selects fallback. Zero, the empty string, the string "0", null, and false also convert to false in conditions. Rust requires a bool and does not implicitly convert collections or…
PHP1<?php2declare(strict_types=1);3$items = [];4$chosen = $items ?: "fallback";5echo $chosen, "\n";6echo ($items ? "true" : "false"), "\n";Rust1fn main() {2 let items: Vec<i32> = Vec::new();3 let present = !items.is_empty();4 println!("{}", present);5 println!("{}", present && items[0] == 9);6}Functions and Returns
PHP can declare parameter and return types, and strict scalar call behavior applies in this source script. An explicit return provides a result; an untyped function without one returns null. Rust signatures state argument and result types, while the final…
PHP1<?php2declare(strict_types=1);3function differs(string $left, string $right): bool { return $left !== $right; }4echo (differs("a", "b") ? "true" : "false"), "\n";5echo (differs("a", "a") ? "true" : "false"), "\n";Rust1fn differs(left: char, right: char) -> bool { left != right }2fn main() {3 println!("{}", differs('a', 'b'));4 println!("{}", differs('a', 'a'));5}Ownership and Moves
PHP array assignment uses value semantics, so modifying the second array leaves the first array values intact. The implementation may share storage until a write, but the observable contract is separate array values. PHP object assignment instead preserves…
PHP1<?php2declare(strict_types=1);3$first = [6, 8];4$second = $first;5$second = array_reverse($second);6echo $first[0], "\n", $second[0], "\n";Rust1fn main() {2 let first = vec![6, 8];3 let mut second = first;4 second.reverse();5 let mut independent = second.clone();6 independent.clear();7 println!("{}", second[0]);8 println!("{}", independent.len());9}Strings and UTF-8
PHP strings are byte sequences, so indexing and strlen do not automatically operate on Unicode characters. The source uses a UTF-8-aware regular expression to select the first code point from valid text, then ord reads its first encoded byte. Rust String and…
PHP1<?php2declare(strict_types=1);3$word = "éx";4preg_match('/^./us', $word, $match);5echo $match[0], "\n";6echo ord($word[0]), "\n";Rust1fn main() {2 let word = "éx";3 println!("{}", word.chars().next().unwrap());4 println!("{}", word.get(0..1).is_none());5}Option and Matching
PHP null coalescing handles an absent key or null value without replacing zero. The source then uses strict null comparisons to distinguish a stored zero from a missing later entry. Rust Option<T> uses Some and None to make presence part of the type, and…
PHP1<?php2declare(strict_types=1);3$values = [0];4$first = $values[0] ?? null;5$missing = $values[2] ?? null;6echo ($first === null ? "true" : "false"), "\n";7echo ($missing === null ? "true" : "false"), "\n";Rust1fn main() {2 let values = vec![0];3 println!("{}", values.get(0).is_none());4 println!("{}", values.get(2).is_none());5}
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 PHP Developers for?
For people who already write PHP and want to learn Rust. Each lesson starts from PHP 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 PHP basics: variables, conditions, loops, functions and collections.
How many lessons does Rust for PHP Developers have?
Rust for PHP Developers contains 48 interactive lessons you can complete at your own pace.
What does Rust for PHP 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 PHP Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.