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 Rust.
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 Wikipedia, 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 book, 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 macros. 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 compiler. All we need to do is drop the code into the editor and hit run.
Alternatively, we can download the latest Rust compiler and a copy of the solution
. Then, assuming the compiler is in the path, navigate to folder with the solution and run the following in Windows:
rustc hello-world.rs hello-world.exe
Of course, in Unix-based environments, the following will run the new binary:
./hello-world
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!
Recent Posts
It seems I'm in my mentorship arc because I can't stop writing about how to support students. Today, we're going to tackle one of the more heartbreaking concerns that students have: the idea that...
For my friends in the humanities, it probably comes as a surprise, but our STEM programs are really allergic to open-ended projects. As a result, I figured I would pitch them today.