DEV Community

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

Posted on

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

1.2.0. Side Note

I will continue using RustRover for demonstrations in future articles.

1.2.1. Writing Rust Programs

  • File extension: .rs

  • Naming convention: Snake case (lowercase letters, words separated by underscores) Example: hello_world.rs

1.2.2. Printing Hello World

Step 1: Create a New Rust Project

Open RustRover, click "New Project", and you'll see the following interface:

Modify the project storage path or select the toolchain location according to your needs, then click create. If the IDE doesn't recognize the toolchain, please check if you have downloaded and installed Rust - there's an installation tutorial on the homepage.

Step 2: Write Code

Since RustRover automatically configures Cargo for new projects (we'll cover this next time), the project will directly generate main.rs with Hello World printing code:

Understanding the code:

fn main(){
    println!("Hello World");
}
Enter fullscreen mode Exit fullscreen mode
  • fn: Indicates creating a function (equivalent to function in JS, func in Go, def in Python)
  • main(){}: main is the function name, () contains parameters (leave empty if none), {} contains the function body. The main function is special - it's the first code executed in every Rust executable program
  • println!();: println!() is the print function, with the content to print inside the parentheses. The ! in the function name indicates this is a macro function, a concept we'll cover later. This macro function call must end with ; because they are equivalent to statements.
  • "Hello World": "" represents a string, Hello World is the content of this string

Note: Rust indentation uses 4 spaces instead of 1 Tab, because Tab has the drawback of displaying differently under different editor configurations - some show 2 characters, some 4 characters, so space indentation is more stable.

Step 3: Run

Simply click the run button in the upper left corner of RustRover (or Ctrl + F5), and you'll see Hello World successfully printed:

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

  • Open terminal, copy the folder path where the .rs file is located, input command cd folder_path to open this folder in terminal.

  • Input command rustc main.rs to compile. If your program name isn't main.rs, you can replace it with your own program name. You'll see two additional files with the same name but different extensions appear in the program directory (Linux/macOS only has one, no .pdb file). The .pdb file is a debug symbol file for Windows platform, and .exe is the executable file.

  • For Windows, input .\main.exe in terminal; for Linux/MacOS, input ./main in terminal. If your program name isn't main, just replace main here with your program name.

Note: Compilation and execution are two separate steps

  • You must compile before running a Rust program, command: rustc your_program_name.rs
  • After successful compilation, a binary file is generated (Windows platform also generates a .pdb file)
  • Rust is an ahead-of-time compiled language, meaning you can compile the program first, then give the executable file to others to run (without needing to install Rust)
  • rustc is only suitable for simple Rust programs, complex Rust programs require Cargo (next chapter will cover this)

Top comments (0)