Hello World with Rust

Now that we have installed Rust on our system, we can start writing our first small program, which is the famous "Hello, World!" program, a tradition in the programming world when learning a new language, which is a simple program that outputs "Hello, World!" to the console.

First things first, let's make a new directory and create a new Rust project:

mkdir hello-world

We will create a new directory named src/ which will contain all the .rs (Rust) files for our project, and we'll have one main.rs file which is the entry point of our Rust program:

$ cd hello-world
 
$ mkdir src
 
$ touch src/main.rs

Now, open the main.rs file in your favorite text editor and write the following code:

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

The println! is called a macro in Rust, a macro is essentially code that writes some other code (more about macros later). The println! macro is used to print text to the console.

Now, let's compile and run our program using rustc. Run the following command in your terminal:

rustc src/main.rs

This will create a new file with the name main (or main.exe on Windows) in the same directory. To run the program, type the following command:

./main

Or if you're on windows, you can navigate to the directory where the main.exe file is located and double-click on it to run the program.

Hello world in Rust Hello world in Rust

Congratulations! 🥳 You have written your first Rust program.

Cargo

We wrote our first program, but there is an easier way to do it. Remember when we installed Rust, we also installed a few tools? One of those tools that you're going to use constantly is cargo, which is Rust's package manager and build system. Cargo makes it easier to manage your Rust projects, and it's the recommended way to create new projects and manage dependencies.

Instead of manually creating the project directories, files, compiling and running the program, we can use Cargo which will do all the work for us.

Let's build exactly the same application we did before, but this time using cargo. Run the following command in your terminal:

cargo new hello-world

This command will create a new Rust project named hello-world with the following structure:

.
├── Cargo.toml
├── .git/
|   ├── ...
├── .gitignore
└── src
    └── main.rs

The Cargo.toml file is the manifest file for your Rust project, it contains all the metadata about your project, such as its name, version, dependencies, etc. (More about this later)

The src/main.rs file is the entry point of your Rust program, and it contains the same code we wrote before:

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

We can now compile and run the program using only one command which is cargo run:

cargo run

Cargo run hello world Cargo run hello world

As you can see, using cargo is much easier and more convenient than using rustc directly. However, there is a bit of logging output that cargo prints when you run the program, which can clutter the output.

The first line it prints Compiling hello-world v0.1.0 (/home/dev/Desktop/hello-world) is the compilation process of your program. The second line Finished dev [unoptimized + debuginfo] target(s) in 0.14s is the completion message of the compilation process.

After that it outputs Running /home/dev/Desktop/hello-world/target/debug/hello-world which is a message that tells you that it's running the compiled binary file of your program.

These logs can be extremely useful, but sometimes you'd want a cleaner terminal. To avoid the clutter, you can run the program with the --quiet, or -q flag:

cargo run --quiet

Cargo run hello world quiet Cargo run hello world quiet

This will only print the output of your program, which is much cleaner.

For the next chapters in this guide, whenever we create a new project, we're going to be using cargo instead of rustc compile and run our programs.

Multiple binaries in Rust

Another heads-up, in the next lessons you might see a flag --bin when running cargo run followed by the name of the binary file. This is because cargo can build multiple binaries in the same project, and you can specify which binary you want to run using the --bin flag.

To have multiple binaries in the same project, you can create a new binary file inside src/bin directory, and then you can run it using cargo run --bin <binary-name>. We're going to discuss managing projects in Rust in more details in the next chapters, but for now, this is just so that you know what does the --bin flag mean.

Here's an example of how you can create a new binary file in the src/bin directory:

$ mkdir src/bin
 
$ touch src/bin/hello-world.rs

And then you can run it using the following command:

$ cargo run --bin hello-world

In the next chapter, we're going to learn about the basic programming concepts that you'll see in any programming language, such as variables, data types, functions, etc. We will dive deeper into these concepts and learn how to use them in Rust.

Rustfinity.com

Links

  1. Home
  2. Learn Rust
  3. Get Started
  4. Practice Rust
  5. Challenges

Socials

  1. GitHub
  2. X

Legal

  1. Privacy Policy
  2. Terms of Service