Rust for Ruby Developers
Build on Ruby objects, collections, blocks and exceptions to learn Rust ownership, borrowing, enums and traits. Complete 48 multi-file tasks, build a parcel depot and port two Ruby programs.
Ruby and Rust side by side
The same program in Ruby and in Rust, from the lessons of this course.
Output and Comments
Ruby runs top-level statements and uses a hash for comments outside strings. Interpolation inside a double-quoted string evaluates the expression, while the remaining hash here is literal text. Rust executable entry starts at fn main and uses slash comments.…
Ruby1# This is a comment.2print "level:"3puts "#{2 + 3} # literal"Rust1fn main() {2 // This line is ignored.3 print!("level:");4 println!("{} # literal", 2 + 3);5}Boolean Conditions
Only false and nil are falsey in Ruby; zero, an empty string and an empty array are truthy. The first source condition therefore selects present even though the array has no elements. Test empty? when emptiness is the actual decision. Ruby && short-circuits…
Ruby1items = []2puts(items ? "present" : "fallback")3puts(!items.empty? && items[0] > 0)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
Ruby methods return their final evaluated expression unless return exits earlier, and ordinary parameters do not declare static types. Rust signatures specify input and result types, with an unterminated final expression supplying the return value. The Ruby…
Ruby1def differs(left, right)2 left != right3end4puts differs("a", "b")5puts differs("a", "a")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
Ruby assignment copies an object reference, so both locals in the source refer to the same mutable Array. reverse! changes that shared object. Rust Vec assignment moves ownership, making the old non-Copy binding unavailable; it does not establish two ordinary…
Ruby1first = [6, 8]2second = first3second.reverse!4puts first[0]5puts first.equal?(second)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
Ruby Strings carry an encoding and may contain invalid byte sequences; their character operations depend on that encoding. In this UTF-8 source, each_char yields the accented character while getbyte reads the first encoded byte. Rust String and str guarantee…
Ruby1word = "éx"2puts word.each_char.first3puts word.getbyte(0)Rust1fn main() {2 let word = "éx";3 println!("{}", word.chars().next().unwrap());4 println!("{}", word.get(0..1).is_none());5}Option and Matching
Ruby Array lookup returns nil when an ordinary index is absent, and an array can also store nil, so a domain may need an additional presence check. Zero is truthy in Ruby, but false and nil are both falsey despite often meaning different things. Rust…
Ruby1values = [0]2puts values[0].nil?3puts values[2].nil?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 Ruby Developers for?
For people who already write Ruby and want to learn Rust. Each lesson starts from Ruby 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 Ruby basics: variables, conditions, loops, functions and collections.
How many lessons does Rust for Ruby Developers have?
Rust for Ruby Developers contains 48 interactive lessons you can complete at your own pace.
What does Rust for Ruby 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 Ruby Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.