Daryl Thayil
Software Engineer
Strategic Partnerships
I’ve been a full stack software engineer for about 6 years. I like to say I am full stack shaded frontend. I love solving real world problems with code. Really enjoying javascript / node, react, and scala right now but I am always open to learning new things.

Creating a basic web service in Rust: Part 1

In part 1, we explore what the Rust language is and how to get a simple hello world application up and running. In part 2 we will set up a web service to return data so we can better understand Rust and the ecosystem around it.

A handful of days each quarter, the engineering team at Porch puts aside its normal sprint work and decides to explore things that interest us. Dev Day’s basic premise is that we attempt to prototype something in a few days that may or may not end up being useful to Porch. Porch has had some awesome features and infrastructure come out of Day Day.

I’ve been pretty interested in picking up a new language that is drastically different than what I normally work with, and Rust has been on the list for a while. For this quarter’s Dev Day, I’m going to try and build a simple web service that returns JSON. Feel free to follow along to see how easy it is to get spun up while figuring out new concepts and tools along the way.

What is Rust?

It sounds corrosive….

Rust is a programming language! The language’s website declares,

“Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.”

And lists the following features:

  • zero-cost abstractions
  • move semantics
  • guaranteed memory safety
  • threads without data races
  • trait-based generics
  • pattern matching
  • type inference
  • minimal runtime
  • efficient C bindings

This all sounds good, but I have never forayed into anything super low level. I’m most comfortable with JavaScript, I work with Java, Scala, and I have experience with a bevy of other high level languages. So my approach to understanding Rust will be from that perspective as opposed to someone coming from a language like C or Go. While Rust is a relatively young language, it has been growing in popularity quickly. Rust was voted the most loved programming language by a Stack Overflow developer survey in 2016.

Hello Rusty World

So now that we have gotten past the intros, let’s jump into the fun stuff.

To start, let’s install Rust. I use Homebrew to manage packages on my Mac.

# Run the following commands once you have brew installed
$ brew update
$ brew install rust

Let’s create a folder somewhere in the dark corners of our machine where hello world apps go to die.

$ cd ~/dark-corner
$ mkdir hello-rust
$ cd hello-rust

# Make our first Rust file
$ touch hello-world.rs

Now let’s put some code in our first Rust file.


fn main() {
    println!("Hello Rusty World");

And then let’s compile and run it.

# Rust compiler = rustc
$ rustc ./hello-world.rs
# compiler creates a file we can execute
$ ./hello-world
# The most magical moment in a hello world journey
Hello Rusty World

So, we got it working! Nothing seems to crazy here, fn main() is the entry point of our application, and we print a string.

What is that exclamation point after println ?

Turns out, it’s a macro. Time for me to find out what that means.


From what I can tell Macros are a sort of short hand, or syntactic sugar, for any block of code you might want to reuse. Macros can be declared using macro_rules!. At compile time macros are expanded into the blocks of code that were declared. This can be useful, when other forms of abstraction don’t make sense, but can also make tracing code more challenging, because you are now dealing with code that has been expanded in line, not written.

Let’s define a macro, a super simple one, and try to use it.

macro_rules! learn_macros {
    () => { println!("Hello Rusty World"); };
    ($name: expr) => { println!("Hello {} World", $name); };

fn main() {

$ rustc ./hello-world.rs
$ ./hello-world.rs
Hello Rusty World
Hello Macro World

Okay, let’s unpack what we just did. When you define a macro, you pass in one or many match functions. The left side of the => defines a pattern to match and the right side of the => defines what to do. The first match we run is an empty match () with no parameters, the second is a match on one expression passed in $name: expr. Looks like function parameters get the $ and define their type in the same way a language like Scala would.

Aside From what I have read, Rust is a mostly typesafe language. You can be unsafe if you want by actually using a keyword called unsafe.

I’m happy being safe for now.

When we call learn_macros! with no parameters, we trigger the first match case and print Hello Rusty World, and when we call learn_macros! with an expression, we get Hello Macro World.

In part 1 we learned about Rust, installed it and looked at some code including a cool feature of the language. In part 2 we will set up a webservice to return data so we can better understand Rust and the ecosystem around it.