Get started learning Rust using the following Hello World examples.
[lwptoc]
Create 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, 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!");
}
Result:
$ rustc main.rs
$ ./main
Hello, world!
Anatomy of a Rust Program
fn main() {
}
These lines define a function in Rust. The main
function is special: it is always the first code that runs in every executable Rust program. 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, ()
.
Also, note that the function body is wrapped in curly brackets, {}
. Rust requires these 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.
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. The Rust team has included this tool with the standard Rust distribution, like rustc
, so it should already be installed on your computer! Check the online documentation for more details.
Inside the main
function is 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 called a function instead, it would be entered as println
(without the !
). For now, you just need to know that using a !
means that you’re calling a macro instead of a normal function.
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.
Compiling and Running Are Separate Steps
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
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.
More Hello World examples
Let us look at more examples:
Example 1: Hello World Rust
This is a simple Hello World Example written in Rust Programming Language.
Step 1: Create Project
- Open your
Rust
IDE. - In the menu go to
File --> Create New Project
.
Step 2: Add Dependencies
Go to your Cargo.toml
and modify it as follows:
No dependencies are needed for this project
Step 3: Write Code
Next create a file known as main.rs
and add the following code:
/*
#1: defines a function named: main
.
main function:
+ this is a special function.
+ it runs as the first code in an executable rust program.
*/
fn main() {
/* ^
|
between {
and }
this is where this function's body starts.
=========================================================
COMPILING & RUNNING THIS PROGRAM:
=========================================================
what you write in the function body will be executed,
1. when you compile:
rustc main.rs
2. and run on xnix:
./main
3. or run on windows:
.\main.exe
*/
println!("Hello, world!");
// ^ ^
/* | +--------------------------------------------------------+
| |
#3: calls a macro named: println
that writes "Hello, world!" to the screen. |
|
+ to call a macro : println!("...") |
+ to call a function: println("...") |
+ you'll learn the differences later on. |
|
|
end your statements with this semicolon. <----------------------------------------+
so the next one can begin.
why?
just like in C, and other languages with a similar syntax,
semicolon here tells the compiler that this statement is over.
*/
}
Run
Copy the code, build and run.
Reference
Here are the reference links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |
Example 2: Hello Wolrd - Run in Docker
Here is the code of main.rs
fn main() {
let hi =say_hi();
println!("{}", hi);
informations::basic();
informations::author();
}
fn say_hi() -> String {
"Hi ! The project is working".to_string()
}
#[test]
fn test_greet() {
assert_eq!("Hi ! The project is working", say_hi())
}
mod informations {
pub fn basic() {
println!("Lets see some basic informations");
}
pub fn author() {
print!("Kevin ");
private_last_name()
}
fn private_last_name() {
print!("Martins");
}
}
Requirements
- Docker
- Docker Compose
Running the project
Here's how you run:
docker-compose build
docker-compose up
Result
hello_1 | Hi ! The project is working
hello_1 | Lets see some basic informations
hello_1 | Kevin Martins
reference
Download the code here.