CodeNewbie Community

loading...
Cover image for Anatomy of a Hello World in Rust

Anatomy of a Hello World in Rust

sentinel1909 profile image Jeff Mitchell ・1 min read

I've decided 2021 is the "year of Rust". I need to learn a modern programming language. Rust is it.

Why? In a word, safety. As I've expressed in previous posts, my coding knowledge is very old. I want to improve it by learning something that's built to help me out. I find Rust's pillar of safety very appealing. I want to be able to create software, but not worry that I'm also creating horrible memory leaks and other nastiness. Yes, I could chose a language that takes me away further from worrying about memory at all (Javascript, C#, etc) but I don't want to be that abstracted away from the bits and bytes.

When learning a new programming language, it's customary to write a program which displays "Hello World". In Rust, this is what the code looks like:

fn main() {
   println!("Hello world!");
}
Enter fullscreen mode Exit fullscreen mode

Let's break it down step by step. First, the main function will appear in pretty much every Rust program you write. It's the starting point of your program's logic. The main function has no parameters and no return value. Alone, it looks like this:

fn main() {

}
Enter fullscreen mode Exit fullscreen mode

The next bit, the only command within the main function, is a Rust macro which does the heavy lifting of our "Hello World" program. It prints the text, "Hello world! on the screen.

That's it! I recommend reading Chapter 1.1, Hello World of the Rust Book for all the juicy details.

Discussion (0)

Forem Open with the Forem app