DEV Community

Cover image for [Rust Guide] 1.2. Basic Understanding of Rust and Printing "Hello World"
SomeB1oody
SomeB1oody

Posted on • Edited on

[Rust Guide] 1.2. Basic Understanding of Rust and Printing "Hello World"

1.2.0 Aside

I strongly recommend using RustRover developed by JetBrains (it is currently free for non-commercial use) as the IDE for writing Rust. I will also continue using RustRover for demonstrations in later articles. This article assumes you already have some programming experience, and C/C++ experience would be even better.

1.2.1 Writing Rust Programs

  • File extension: .rs

  • Naming convention: snake case, using lowercase letters and underscores to separate words
    Example: hello_world.rs

1.2.2 Printing Hello World

Step 1: Create a New Rust Project

Open RustRover and click New Project. You will see the following screen:

Change the project save path or choose the location of the toolchain according to your needs, then click Create. If the IDE does not recognize the toolchain, check whether Rust has been downloaded and installed. The installation guide is on the homepage.

Step 2: Write the Code

Because RustRover automatically configures Cargo for new projects (which will be covered in the next article), the project will directly generate main.rs and include code for printing Hello World:

Understanding the code:

fn main(){
    println!("Hello World");
}
Enter fullscreen mode Exit fullscreen mode
  • fn: indicates that a function is being created (equivalent to function in JS, func in Go, and def in Python)

  • main(){}: main is the function name. The () contains parameters; if there are none, nothing is written. The {} contains the function body. The main function is special: it is the first code executed by every Rust executable program

  • println!();: println!() is the print function. The parentheses contain the content to print. The ! in the function name means this is a macro function, which will be covered later. This macro call must end with ; because it behaves like a statement.

  • "Hello World": "" represents a string, and Hello World is the content of that string

Note: Rust indentation uses 4 spaces instead of 1 tab. The reason is that tabs have a drawback: they can appear differently depending on editor settings; some use 2 spaces, some use 4 spaces, so space indentation is more stable.

Step 3: Run

Simply click the Run button in the top-left corner of RustRover (or press Ctrl + F5) and you will see Hello World printed successfully.

For non-RustRover users, you can also run the program through Terminal:

  • Open the terminal, copy the folder path containing the .rs file, and enter cd folder_path to open that folder in the terminal.

  • Enter rustc main.rs to compile. If your program file is not named main.rs, you can replace it with your own file name. You will see two extra files with the same name but different extensions in the directory where the program is located (on Linux/macOS, there is only one and no .pdb file). The .pdb file is a Windows debugging symbol file, and .exe is the executable file.

  • For Windows, enter .\main.exe in the terminal; for Linux/macOS, enter ./main. If your program is not named main, just replace main with your program name.

Note: compilation and execution are two separate steps

  • Before running a Rust program, you must compile it first with rustc your_program_name.rs
  • After successful compilation, a binary file will be generated (on Windows, a .pdb file will also be generated)
  • Rust is an ahead-of-time compiled language, which means you can compile the program first and then hand the executable to someone else to run without installing Rust
  • rustc is suitable only for simple Rust programs; complex Rust programs need Cargo (which will be discussed in the next chapter)

Top comments (0)