5 min read

While most people come into Rust from a background of c++, systems programming, low latency, and tight memory environments, I came here after most of my life learning Ruby and being drawn to the elegance it came with. I know much of this is strictly inspired by Ruby, but I am encountering so much Rust syntax that makes me feel quite at home.

Implicit Returns

One of my favorite things about ruby is small functions not needing an explicit return keyword which felt so neat and natural.

Rust:

fn add(x: i64, y: i64) -> i64 {
    x + y
}

and similar ruby:

def add(x, y)
  x + y
end

Using Expressions On Assigment

This one is quite minor but I’ve really liked this because I don’t have to keep declaring variables outside an if/switch block

Rust:

let score = 43;
let grade = if score == 50 {
    "Pass"
} else {
    "Fail"
};

Ruby:

score = 43
grade = if score > 50
  "Pass"
else
  "Fail"
end

Switch flow with match

I love having a neat switch case like flow that avoids the C style fall through errors

Rust with match and compiler even ensures my ranges don’t overlap

let score = 42;
let grade = match score {
    50.. => "Pass",
    42 => "I'll make an exception",
    _ => "Fail"
};

Ruby with case and when

score = 42
grade = case score
        when (50..)
          "Pass"
        when 42
          "I'll make an exception"
        else
         "Fail"
        end

Range syntax using ..

Being able to start..end ranges has always been so cute, and even nicer seeing unbounded also work.

Rust:

let exclusive_range = 1..3;
assert_eq!(1+2, exclusive_range.sum());

let inclusive_range = 1..=3;
assert_eq!(1+2+3, inclusive_range.sum());

let unbounded_range_end = 100..;
let unbounded_range_start = ..99;
println!("{:?} {:?}", unbounded_range_end, unbounded_range_start);

Ruby:

exclusive_range = 1...3
inclusive_range=1..3
puts "1 + 2 = #{exclusive_range.sum}"
puts "1 + 2 + 3 = #{inclusive_range.sum}"

unbounded_range_end = 100..;
unbounded_range_start = ..99
puts "#{unbounded_range_end} #{unbounded_range_start}"

# yes on rare occasions ruby does need ;, we could also use () to wrap or explicitly set `nil` as the end but the issue here is ruby is looking on the next line for the range end

Note that the ruby and rust inclusive and exclusive are a flipped here, and there was quite a bit of debate around this back in the day

Also interestingly, some versions of rust allow ... similar to ruby having ..., but only in expressions and with some deprecation warnings suggesting using ..= which does honestly read better imo.

let age = 26;
match age {
    0...26 => println!("still cool"),
    _ => println!("unc"),
}

Passing Blocks around with |pipe| arguments

let nums = vec![1, 2, 3];
let odd: Vec<i64> = nums.into_iter().filter(|item| item % 2 == 1).collect();

Nice Higher Order Helpers

I really got used to having nice higher order functions that work like map/filter/reduce and rust feels just at home

Rust

let nums = vec![1, 2, 3];
let odd_sq_product = nums.into_iter()
    .filter(|item| item % 2 == 1)
    .map(|item| item*item)
    .fold(1, |acc, item| acc * item);

Ruby

nums = [1, 2, 3];
odd_sq_product = nums.select {|item| item % 2 == 1}
  .map {|item| item * item}
  .reduce {|acc, item| acc * item}

There’s definitely more

I’m still quite new to Rust, so I may add on a few more things as I run across them.

GitHub Repo

I’ve also placed all these code samples from writing this post into a GitHub repo if anyone wants to download anything to play around, but it’s basically the same.