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:
.rsNaming 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");
}
fn: indicates that a function is being created (equivalent tofunctionin JS,funcin Go, anddefin Python)main(){}:mainis the function name. The()contains parameters; if there are none, nothing is written. The{}contains the function body. Themainfunction is special: it is the first code executed by every Rust executable programprintln!();: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, andHello Worldis 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
.rsfile, and entercd folder_pathto open that folder in the terminal.
- Enter
rustc main.rsto compile. If your program file is not namedmain.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.pdbfile). The.pdbfile is a Windows debugging symbol file, and.exeis the executable file.
- For Windows, enter
.\main.exein the terminal; for Linux/macOS, enter./main. If your program is not namedmain, just replacemainwith 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
.pdbfile 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
-
rustcis suitable only for simple Rust programs; complex Rust programs need Cargo (which will be discussed in the next chapter)






Top comments (0)