Introduction
Welcome to Hello, World. This lab is a part of the Rust Book. You can practice your Rust skills in LabEx.
In this lab, you will write your first Rust program, which will print the text "Hello, world!" to the screen.
This tutorial is from open-source community. Access the source code
Welcome to Hello, World. This lab is a part of the Rust Book. You can practice your Rust skills in LabEx.
In this lab, you will write your first Rust program, which will print the text "Hello, world!" to the screen.
Now that you've installed Rust, it's time to write your first Rust program. It's traditional when learning a new language to write a little program that prints the text Hello, world!
to the screen, so we'll do the same here!
Note: This book assumes basic familiarity with the command line. Rust makes no specific demands about your editing or tooling or where your code lives, so if you prefer to use an integrated development environment (IDE) instead of the command line, feel free to use your favorite IDE. Many IDEs now have some degree of Rust support; check the IDE's documentation for details. The Rust team has been focusing on enabling great IDE support via
rust-analyzer
. See Appendix D for more details.
You'll start by making a directory to store your Rust code. It doesn't matter to Rust where your code lives, but for the exercises and projects in this book, we suggest making a project
directory in your home directory and keeping all your projects there.
Open a terminal and enter the following commands to make a project
directory and a directory for the "Hello, world!" project within the project
directory.
For Linux, macOS, and PowerShell on Windows, enter this:
cd ~/project
mkdir hello_world
cd hello_world
Next, make a new source file and call it main.rs
. Rust files always end with the .rs
extension. If you're using more than one word in your filename, the convention is to use an underscore to separate them. For example, use hello_world.rs
rather than helloworld.rs
.
Now open the main.rs
file you just created and enter the code in Listing 1-1.
Filename: main.rs
fn main() {
println!("Hello, world!");
}
Listing 1-1: A program that prints Hello, world!
Save the file and go back to your terminal window in the ~/project/hello_world
directory. On Linux or macOS, enter the following commands to compile and run the file:
$ rustc main.rs
$ ./main
Hello, world!
Regardless of your operating system, the string Hello, world!
should print to the terminal. If you don't see this output, refer back to "Troubleshooting" for ways to get help.
If Hello, world!
did print, congratulations! You've officially written a Rust program. That makes you a Rust programmer---welcome!
Let's review this "Hello, world!" program in detail. Here's the first piece of the puzzle:
fn main() {
}
These lines define a function named main
. The main
function is special: it is always the first code that runs in every executable Rust program. Here, the first line declares a function named main
that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses ()
.
The function body is wrapped in {}
. Rust requires curly brackets around all function bodies. It's good style to place the opening curly bracket on the same line as the function declaration, adding one space in between.
Note: If you want to stick to a standard style across Rust projects, you can use an automatic formatter tool called
rustfmt
to format your code in a particular style (more onrustfmt
in Appendix D). The Rust team has included this tool with the standard Rust distribution, asrustc
is, so it should already be installed on your computer!
The body of the main
function holds the following code:
println!("Hello, world!");
This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.
First, Rust style is to indent with four spaces, not a tab.
Second, println!
calls a Rust macro. If it had called a function instead, it would be entered as println
(without the !
). We'll discuss Rust macros in more detail in Chapter 19. For now, you just need to know that using a !
means that you're calling a macro instead of a normal function and that macros don't always follow the same rules as functions.
Third, you see the "Hello, world!"
string. We pass this string as an argument to println!
, and the string is printed to the screen.
Fourth, we end the line with a semicolon (;
), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.
You've just run a newly created program, so let's examine each step in the process.
Before running a Rust program, you must compile it using the Rust compiler by entering the rustc
command and passing it the name of your source file, like this:
rustc main.rs
If you have a C or C++ background, you'll notice that this is similar to gcc
or clang
. After compiling successfully, Rust outputs a binary executable.
On Linux, macOS, and PowerShell on Windows, you can see the executable by entering the ls
command in your shell:
$ ls
main main.rs
From here, you run the main
file, like this:
./main
If your main.rs
is your "Hello, world!" program, this line prints Hello, world!
to your terminal.
If you're more familiar with a dynamic language, such as Ruby, Python, or JavaScript, you might not be used to compiling and running a program as separate steps. Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed. If you give someone a .rb
, .py
, or .js
file, they need to have a Ruby, Python, or JavaScript implementation installed (respectively). But in those languages, you only need one command to compile and run your program. Everything is a trade-off in language design.
Just compiling with rustc
is fine for simple programs, but as your project grows, you'll want to manage all the options and make it easy to share your code. Next, we'll introduce you to the Cargo tool, which will help you write real-world Rust programs.
Congratulations! You have completed the Hello, World lab. You can practice more labs in LabEx to improve your skills.