From the course: Rust Essential Training

Anatomy of a Rust program - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Anatomy of a Rust program

- [Instructor] When learning a new programming language it's traditional to start by writing a simple program that displays the message, Hello, World! on the console. We do that to verify that our tools are installed correctly so we can successfully compile and run a program. And it provides a simple entry point to begin learning about the basic structure and syntax of a Rust program. To begin, I'll create a new file in VS code, like going to File, New File, Then I'll go to File, Save As, and I'll name it main.rs. Rust files containing source code always end with the extension.rs, and although not absolutely required here, it's common to name the primary source file, main. The requirement for source file names is that they do not contain spaces. So, if I wanted to name this file, hello, world, I can name it, hello _world. But for now let's stick with the convention of main.rs and for simplicity, I'll save this to the Desktop. Now we can begin writing code. The first thing we'll do is type the keyword fn to define a new function. We'll name that function main followed by open enclosed parentheses and then enclosed curly braces, which will separate out onto different lines. Every Rust program needs to have a top level function named main, which is defined like this because it serves as the entry point into the program. When we compile and run our program it will start by executing whatever code we put within the curly braces of this main function. If we don't include a main function, our program won't know where to begin execution. We'll cover how to define and use other functions in more detail later in this course but for now notice that the parentheses after main are empty indicating the main function does not accept any inputs. Now, for the real meat of our program inside the main function we'll type four spaces println! which is sometimes called a bang open parentheses double quotes, Hello, world! And at the end of that a semi-colon this single line will print the text Hello, world! To the console. And there are a few things I want to point out about it. First notice that I used four spaces to indent the code within the main function, Rust is what's known as a free form language, which means how the code is spaced out and positioned on the page, doesn't really matter. We can write this code with the print statement indented or not using indentation to organize the code helps make it easier to read as a human programmer but to the Rust compiler, that spacing doesn't matter. We could even write this program on a single line. The compiler would parse this code to mean the same thing as before though, as a human it's getting tough to read. When it comes to and indenting code programmers will occasionally get into heated arguments about whether it's better to use tabs or spaces. The conventions do vary among languages but when programming in Rust it's standard practice to indent lines with multiples of four spaces. So that's what we'll use here. The next thing to notice is that the message Hello, world! Is contained within double quotes which identifies it as a string literal. It's the sequence of characters we want to display that string is contained within a pair of parentheses to pass it as an input to print line. The values we pass as inputs are often called arguments. So in this case the Hello, world! String is an input argument for print line. The exclamation Mark indicates that print line is something called a macro which is an advanced topic beyond the scope of this course but in the way we're using it here you can think of it like a function. When the print line macro runs it will display our Hello, world! String on the console followed by an invisible new line character at the end. Finally, at the end of that line of code we use a semi-colon to indicate that this statement is complete so we can potentially start another one. You'll notice that most of the lines of code we write and Rust will end with a semi-colon but not always as we'll see later in this course whether or not you end a line with a semi-colon changes its behavior that covers everything so I'll save our program and then to compile it I'll open a new terminal within VS code. They going to Terminal, New Terminal. This terminal is currently navigated to the Desktop as the working directory, and if I use the LS command to list the directory contents I see that main.rs is the only file here. So I'll call the Rust compiler with Rust C followed by main.rs as the source file to compile. And after the compiler finishes I'll use the LS command again. And now I see there are three files on my Desktop. There's the original .rs source file. There's a .PDB file which contains debugging information that was generated because I'm using Windows and main.exe which is the executable output file that was generated to run it, I'll type period backslash main.exe. And there you have it, our message, Hello, world! So that's Hello, world! We've successfully written our first Rust program learned a bit about how it structured and proven that our compiler works.

Contents