1.2.0. Side Note
I will continue using RustRover for demonstrations in future articles.
1.2.1. Writing Rust Programs
File extension:
.rsNaming 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");
}
-
fn: Indicates creating a function (equivalent tofunctionin JS,funcin Go,defin Python) -
main(){}:mainis the function name,()contains parameters (leave empty if none),{}contains the function body. Themainfunction 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 Worldis 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
.rsfile is located, input commandcd folder_pathto open this folder in terminal.
- Input command
rustc main.rsto compile. If your program name isn'tmain.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.pdbfile). The.pdbfile is a debug symbol file for Windows platform, and.exeis the executable file.
- For Windows, input
.\main.exein terminal; for Linux/MacOS, input./mainin terminal. If your program name isn'tmain, just replacemainhere 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
.pdbfile) - 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)