[Rust] Hello World
This post is written by YoungJ-Baek
This post is written referring to the official documentation
This post is based on M1 Macbook Pro 14 inch, macOS Ventura 13.0
1. Preface
Printing Hello, world! is usually very first step of learning programming language. So, today, I will introduce how to print our bible.
2. Print Hello World
2.1. Prepare Project Folder
First, open your terminal, and then type the command below to make project folder. We will use this folder many time during learning Rust, so I recommend to make it this time.
Command
$ mkdir projects
$ cd projects
$ mkdir hello_world
$ cd hello_world
2.2. Create Main file
Second, create main file in the directory. If you have learned C/C++ or Python before, it is same with main.c/main.cpp or main.py. Rust uses .rs as an extention. After you have generated the file, type the code below.
main.rs
fn main() {
println!("Hello, world!");
}
Now, you are ready to print Hello, world.
2.3. Compile and Run
Third, you are now ready to compile the file and run it. In C/C++, you can do this via make command. In Rust, you can do this with rustc command instead.
Compile and Run
$ rustc main.rs
$ ./main
Result
Hello, world!
3. Deep Dive
Now, you have printed Hello, world with Rust. You can find some differences compared to other languages, especially C++.
Rustuses4 spacesinstead oftabto indentprintln!callsRust macro. If you want to call a function namedprintln, call it without!. More details about it will be described later- Like
C++, we use;to end a line
Moreover, we have learned rustc works like make in C/C++. So, we can compile and run the file using this command. However, if you can remember the cons of make command, I am sure that you wish there may be a command like cmake to compile multiple files or entire project. Fortunately, Rust has that option. I will introduce it at the very next post.
Leave a comment