Your first Rust application
In this Rust tutorial we learn to write, compile and run our very first Rust application. We also provide a quick breakdown of what we've written to help you understand how a basic Rust program works.
Hello World
Now that we’re all set up, we can start writing our first Rust application.
Launch your IDE and open the main.rs file in the src folder of your ‘tutorial_project’ project.
There may be some starting code already in this file. Select all of it and press backspace on your keyboard to delete it.
Write the following code.
It’s important to code along with the tutorials, or at least run the examples. Coding along helps familiarize you with the syntax and remembering the expressions.
fn main() {
// My first Rust app
let message = "Hello World";
println!("{}\n",message);
}
That’s it, that’s a very simple Rust application. All we need to do now is compile and run it.
Compile and run in IntelliJ IDEA:
In the Project Explorer, right-click on the main.rs file and chose Run ‘tutorial_project’.
Compile and run in Eclipse:
In the Project Explorer, right-click on the main.rs file and choose Run As > Cargo Based Rust Project to compile and run the application.
Output:
In the Console window below the Code Editor, we will see the words ‘Hello World’.
Even if we close the Console window, each time we compile the program it will reappear below the Code Editor.
Hello World
There will be more lines above that, which simply show us that the application was compiled, and is currently running the application tutorial_project.exe .
The words ‘Hello World’ is the output from the application itself. That is what the tutorial_project.exe would output in its console window.
Because we’re using an IDE like IDEA or Eclipse, we don’t have to go back and forth between the editor and the compiled application. We can see it directly in the Console window.
Breakdown of your first Rust application
Congratulations! You’ve just written, compiled and ran your first Rust application.
Now, let’s do a quick step-by-step breakdown of the simple Rust application we just wrote, so you can understand how it works.
We will expand on the following concepts in more depth throughout the rest of the tutorial course.
Functions
The first and last lines make up a function declaration.
fn main() {
}
A function is a way for us to group sections of code for reuse throughout our program. Any code we want to execute is placed between the open and close curly braces.
The main() function is required once in any executable Rust program and acts as a starting point for our code.
Comments
The next line is a comment.
fn main() {
// My first Rust app
}
A comment is a way for a developer to document their code to improve its readability. Comments are ignored by the compiler.
A single line comment has a prefix of two forward slashes. Anything after the forward slashes is considered a comment by the compiler.
Data Containers (variables, constants etc.)
Following the comment, we have a temporary data container called a variable.
fn main() {
// My first Rust app
let message = "Hello World";
println!("{}\n",message);
}
A variable will store an application’s temporary data while it’s running. In this case, we store a string of characters that make up the words “Hello World”.
Strings require quotes around them.
Macros
The next statement in the application is a macro, which looks almost similar to a function call.
fn main() {
// My first Rust app
let message = "Hello World";
println!("{}\n",message);
}
A macro is like an advanced function. It can do things that regular functions are not able to, like encapsulate patterns, declare types that match a template etc. Macros are also fast and type safe.
In this case, the macro prints the words stored in our message variable to the console.
We identify it as a macro with the exclamation point operator. Macro calls will have an exclamation point, and function calls will not.