Writing first Rust program
Before writing a program in rust, lets look up some necessosary keywords, recommendations and steps :
- rustup : A tool to manage rust and its tools.
- rustc : A rust compiler that converts rust source code into executable.
- rustfmt : A rust tool to make your source code pretty and clear. (fmt ⟶ Format)
- rust-analyzer : A tool for some IDEs to make developer experience better.
- Always create source code file with .rs extention and snake_case naming style.
- Create a function main in source code file
Program execution
To execute a program you have to :
- Create a file with .rs extention. (ex: hello_world.rs)
- Make a main function within that file then save that file.(mendatory)
- Use rustc compiler to create a executable file of source file. rustc hello_world.rs It will create a executable file with same name.
- Run the executable file.
Project Setup
It is necessosary to write code in a structured format instead of writing code in different directories or throwing files on desktop.
- Create a directory which you uses for practicals along with your rust code journey. (ex: mkdir rust_practicals)
- Make meaningfull source file name with recommended snake_case
- You can create more child directories for more ease when you make the journey longer.
- If you are using IDE, then make sure to install rust-analyzer for more ease.
Writing Hello World!! program
Lets write our first hello world program in rust using following steps :
- Step 1 - Create a directory for our all rust source code and practicals and open it in any IDE/editor. (mac/linux : mkdir rust_practicals && cd rust_practicals)
- Step 2 - Create a new file with .rs extention. ex : hello_world.rs
- Step 3 - Create a main function that prints "Hello World!!" to output. hello_world.rs rust
fn main() { println!("Hello World!!"); }
- Step 4 - Use rustc to make executable file using above source code file.
rustc hello_world.rs - Step 5 - Run the executable. (mac/linux: ./hello_world, Powershell: .\hello_world.exe)
- Now you can see the output Hello World!! on your screen.
Remember the flow : source_code ⟶ compiler ⟶ executable ⟶ run ⟶ output
Frequently Asked Questions
What is the first step to write a Rust program?➕
The first step to write a Rust program is to create a '.rs' file (ex: , 'hello_world.rs') and define a 'main' function that will be the entry point of your program.
How do I compile and run a Rust program?➕
You can compile a Rust program using the 'rustc' command: 'rustc filename.rs'. This creates an executable that you can run directly (./filename' on Linux/macOS or '.\filename.exe' on Windows).
What tools are needed to start Rust development?➕
Essential Rust tools include 'rustup' (for managing Rust versions), 'rustc' (compiler), 'rustfmt' (for formatting code), and 'rust-analyzer' (for IDE support and smart suggestions).
What file extension is used for Rust source files?➕
Rust source files use the '.rs' file extension. It's recommended to follow the 'snake_case' naming convention for file names.
Why should I use a directory structure for Rust projects?➕
Using a structured directory (ex: , 'rust_practicals/') helps you organize your code, separate different projects, and manage files more effectively, especially as your Rust journey grows.
Do I need an IDE to write Rust code?➕
No, an IDE is not required, but using one (like VS Code with 'rust-analyzer') significantly enhances productivity with features like syntax highlighting, auto-completion, and real-time error detection.
What is the purpose of the 'main()' function in Rust?➕
The 'main()' function is the entry point of any standalone Rust program. It defines what should happen when the compiled executable is run.
Can I write and run Rust programs on any operating system?➕
Yes, Rust is cross-platform and works on Linux, macOS, and Windows. The installation methods and executable commands might vary slightly by OS.