Rust for Java Developers
Move from Java references, classes and exceptions to Rust ownership, borrowing, traits and explicit errors. Complete 48 multi-file tasks, build a parcel depot, and port two Java applications.
Java and Rust side by side
The same program in Java and in Rust, from the lessons of this course.
Output and Comments
Java starts this program in Main.main(String[] args). Rust starts in a free fn main(), with no enclosing class. Both languages accept // line comments and /* ... */ block comments. A comment is ignored by the compiler, not printed.
Java1class Main {2 public static void main(String[] args) {3 // Addition happens after a String has been produced.4 System.out.println("sum=" + 2 + 3);5 }6}Rust1fn main() {2 // Addition is evaluated before formatting.3 println!("sum={}", 2 + 3);4}Boolean Conditions
Java and Rust conditions require booleans. Neither treats an integer as a truth value. Both use && and || with short-circuit evaluation, and ! negates a boolean. With left || right, the right expression is not evaluated if the left is already true. With left…
Java1class Main {2 public static void main(String[] args) {3 int divisor = 0;4 boolean bypass = divisor == 0 || 12 / divisor > 2;5 System.out.println(bypass);6 }7}Rust1fn main() {2 let divisor: i32 = 0;3 let bypass = divisor == 0 || 12 / divisor > 2;4 println!("{}", bypass);5}Functions and Returns
Java methods belong to classes; a static method can be called without an instance. Rust free functions are module items. Their arguments have explicit types, and -> T states the result type. A trailing expression returns its value without the return keyword.…
Java1class Main {2 record Pair(int first, int second) {}3 static Pair swap(int a, int b) { return new Pair(b, a); }4 public static void main(String[] args) {5 Pair p = swap(4, 9);6 System.out.println(p.first() + ":" + p.second());7 }8}Rust1fn swap(a: i32, b: i32) -> (i32, i32) { (b, a) }2fn main() {3 let (first, second) = swap(4, 9);4 println!("{}:{}", first, second);5}Ownership and Moves
Java assignment of an object reference creates another reference to the same object. The garbage collector tracks reachability; assigning or passing that reference does not invalidate the original variable. Rust owned values follow a different rule. Assigning…
Java1class Main {2 public static void main(String[] args) {3 StringBuilder first = new StringBuilder("reed");4 StringBuilder second = first;5 second.setCharAt(0, 's');6 System.out.println(first + ":" + second);7 }8}Rust1fn main() {2 let first = String::from("reed");3 let mut second = first.clone();4 second.replace_range(0..1, "s");5 let owner = second;6 println!("{}:{}", first, owner);7}Strings and UTF-8
Java String uses UTF-16 code units: length() counts those units, and a char may be only half of a supplementary character. codePointAt and codePoints() work with Unicode code points. Rust String and str contain valid UTF-8. len() counts bytes, while chars()…
Java1class Main {2 public static void main(String[] args) {3 String s = "𝄞a";4 System.out.println(s.codePointAt(0));5 }6}Rust1fn main() {2 let s = "𝄞a";3 println!("{}", s.chars().next().unwrap() as u32);4}Option and Matching
Java reference variables can contain null, and dereferencing one can throw NullPointerException. Optional<T> makes some absence contracts explicit, although Java still permits a reference to an Optional itself to be null. Rust Option<T> is an enum with…
Java1class Main {2 public static void main(String[] args) {3 Integer[] data = {6};4 java.util.Optional<Integer> value = data.length > 2 ? java.util.Optional.of(data[2]) : java.util.Optional.empty();5 System.out.println(value.orElse(40));6 }7}Rust1fn main() {2 let data = [6];3 let value = data.get(2);4 println!("{}", value.copied().unwrap_or(40));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 Java Developers for?
For people who already write Java and want to learn Rust. Each lesson starts from Java 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 Java basics: variables, conditions, loops, functions and collections.
How many lessons does Rust for Java Developers have?
Rust for Java Developers contains 48 interactive lessons you can complete at your own pace.
What does Rust for Java 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 Java Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.