Hello World in Rust

Hello World in Rust Featured Image

We’ve made it! We’re done with our first 10 languages for the Hello World in Every Language Series. So far, we’ve covered Python, C, C++, Java, Ruby, Swift, Objective-C, Go, and C#. For our tenth language, we’re implementing Hello World in Rust, a language sponsored by Mozilla.

Table of Contents

Rust Background

At this point, it’s probably no surprise that I took to Wikipedia to learn more about RustOpens in a new tab..

According to Wikipedia, Rust is our first systems programming language. Up until now, we’ve really only played with general-purpose languages, so this is a nice change of pace. That said, what is a systems programming language?

Again, according to WikipediaOpens in a new tab., systems programming languages are designed for system software like device drivers and operating systems. Back when I took a compilers course, the professor used Rust for their example compiler, so I guess that checks out.

With that said, what else can we find out about Rust? Well, Rust first showed up on the scene back in 2010 as a Mozilla sponsored project. Today, a large portion of the language has been written by the open source community. How cool is that?

In terms of syntax, Rust resembles languages like C and C++, but it borrows quite a lot from a general-purpose function programming language called ML. In addition, the language contains features like memory safety and ownership.

Hello World in Rust

Like our last lesson on Go, Hello World in Rust is very similar to Hello World in C:

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

In fact, Rust’s implementation is even easier. There’s no need to import any IO packages to get access to println. We just need to create our main function, add our print code, and we’re done.

But, wait a minute. That print line seems a little off. What’s with the bang !? To be honest, I had to do a bit of digging for this. As it turns out, println is not a function at all. It’s a built-in macro. That’s a new term for me, so let’s learn a little more about it.

According to the Rust Programming Language bookOpens in a new tab., macros are a language feature that allow you to abstract syntax. In other words, macros allow you to do some metaprogramming by adding grammar to Rust’s abstract syntax tree. Perhaps an example would make more sense:

macro_rules! println {
    () => (print!("\n"));
    ($fmt:expr) => (print!(concat!($fmt, "\n")));
    ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));

This is the actual definition of the println macro in Rust. I won’t go into exactly what’s happening, but basically we have defined three available patterns for println: empty, one argument, and variable arguments. Rust functions don’t have support for variable arguments, so you can add the functionality with macros.

That said, I’m only just learning macros for the first time, so I recommend an article by Kasper Anderson called why Rust has macrosOpens in a new tab.. It’s quite thorough, and I think it does a better job than Rust’s documentation.

How to Run the Solution

As always, we can try out this code using an online Rust compilerOpens in a new tab.. All we need to do is drop the code into the editor and hit run.

Alternatively, we can download the latest Rust compilerOpens in a new tab. and a copy of the solutionOpens in a new tab.. Then, assuming the compiler is in the path, navigate to folder with the solution and run the following in Windows:

rustc hello-world.rs

Of course, in Unix-based environments, the following will run the new binary:


And, that’s it! “Hello, World!” should print directly to the console.

Sample Programs in Every Language

Phew! That was a tough one for me. While I’m familiar with compilers, macros are brand new to me. I wasn’t expecting to struggle this much with a simple Hello World program. In fact, I’ve found that even some of the esoteric languages like Brainf*ck are easier comprehend. But, who doesn’t love a challenge? I imagine I’ll run into plenty of these kind of road blocks on this journey.

At any rate, thanks for stopping by. Up next, we’ll be moving to the web programming realm with an implementation of Hello World in JavaScript. After that, we’ll tackle PHP. If you loved this article or you want to see more, let me know in the comments. It’s the only way I can get better!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts