DEV Community

Cover image for Rust Hello World: The Hard and the Smart Way
Francesco Ciulla
Francesco Ciulla

Posted on

Rust Hello World: The Hard and the Smart Way

How do we make a Hello World in Rust??

If you prefer watching over reading, you can watch the video version of this bootcamp segment right here:


The Prerequisites

The first thing you should do is install Rust. You should end up on a page that guides you through using rustup, which is an excellent tool for managing Rust versions.

To verify your installation, type rustc --version in your terminal.

Rust is a compiled programming language. rustc is the compiler, and cargo is the package manager. If you have both of these, you are good to go.

The "Hard Way" (Manual Compilation) 🔨

We will start with a very simple "Hello, World!" because this step is very important to understand what happens behind the scenes.

This approach uses rustc directly. This is something you usually don't see in tutorials, but it helps to understand what automated tools do for us later.

  1. Create a new folder and construct a file named main.rs (.rs is the extension for Rust files).
  2. Open it in your favorite IDE (VS Code, RustRover, etc.) and add the code:

    fn main() {
        println!("Hello, world!");
    }
    
  3. How do you run this? There isn't a play button. You need to compile it.
    Run this command in your terminal:

    rustc main.rs
    
  4. This compiles the code and creates an executable in your folder (e.g., just main on Linux/macOS or main.exe on Windows).

  5. Run the executable:

    ./main
    # Output: Hello, world!
    

Why is this the "Hard Way"?
If you make a change to the file—for example, adding a crab emoji 🦀 to the string—and save it, running ./main again won't show the change. You must manually run rustc main.rs again to recompile the changes every single time. This is obviously not ideal for real development.

I've done this just because I want you to understand what cargo does behind the scenes.

The "Smart Way" (Cargo) 🚀

Usually, you will initialize a Rust project using Cargo. Cargo is basically the npm of the Rust world.

Let's initialize a new project the standard way:

  1. Run the following command to create a new project:

    cargo new rust_live_one
    

    Cargo will create the directory structure for you. It creates a src folder containing main.rs, sets up a git repository, and creates a Cargo.toml file (which is similar to package.json in JS or requirements.txt in Python).

  2. To run this project, you don't use rustc. You use:

    cargo run
    

If you change your code and run cargo run again, Cargo realizes the code has changed and recompiles the necessary files automatically before running it. Thank you, Cargo!

🦀 Pro Tip: Quiet Mode

By default, Cargo prints outputs like "Compiling..." and "Finished..." before showing your program's output.

If you want the exact same clean output we had with the manual method, use the quiet flag:

cargo -q run
Enter fullscreen mode Exit fullscreen mode

Here is a handy cheat sheet summarizing the setup process (click the image to watch the video!):

hello world infographic

If you prefer watching over reading, you can watch the video version of this bootcamp segment right here:

Top comments (0)