Writing Your First Rust Program Using Following Steps
Step 1: Create a New Rust Project
Cargo is used for set the project easily. Firstly set up your project with cargo:
cargo new hello_world
cd hello_world
After cargo will creates the following structure, Understand this structure:
hello_world/
├── Cargo.toml # Project configuration file
└── src/
└── main.rs # Main source file
What is Cargo in Rust?
Cargo is the official package manager and build system for Rust. cargo is similar to npm (Node.js), pip (Python) or Maven (Java) but specifically designed for Rust. cargo managing Rust projects by handle dependencies, compiling code, running tests and generating documentation.
Cargo will automatically downloads and manages external Rust libraries and it is (called crates) from Crates.io. all the dependencies are defined in the Cargo.toml file.
Step 2: Open main.rs
After navigate to the src folder and open main.rs file. this file contains the following default code:
fn main() {
println!("Hello, world!");
}
Step 3: Run the Program
To compile and execute the program, use this command for run the program:
cargo run
Output:
Hello, world!
Breaking Down the Code
- fn main():
- fn declares a function in Rust.
- main() is the entry point of every Rust program.
- println!():
- This is a macro in Rust and it is (indicated by the
!
). - It prints the text inside the parentheses to the console and followed by a newline.
- “Hello, world!” is the string being printed.
- This is a macro in Rust and it is (indicated by the
- Semicolons ( ; ):
- Rust uses semicolons to terminate statements. omitting it will result in a compilation error.
What Are Macros in Rust?
In Rust macros are the write reusable, flexible and powerful code patterns that can generate code at compile time. macros allow developers to avoid repetitive code while keeping the program efficient. macros in Rust totally different from functions because they can accept variable-length arguments and perform metaprogramming tasks.
Types of Macros in Rust:
- Declarative Macros (macro_rules!)
- Procedural Macros
- Builtin Macros
Coding Example: Customizing “Hello, World!”
You can modify the message according to yourself and it will give the output with all the changes:
fn main() {
println!("Hello, Rust Learners!");
}
Output:
Hello, Rust Learners!
Compiling Without Cargo
While Cargo continues the process, you can also compile manually without cargo:
For compiling manually, first save the following code in a file named main.rs and following below commands:
fn main() {
println!("Hello, world!");
}
after compile the program using rustc command in terminal:
rustc main.rs
Run the executable:
./main
Output:
Hello, world!
Key Concepts to Remember
- Macros: Rust uses macros like println!() for functionality.
- Static Typing: But it is not visible in this example because Rust is a statically-typed language.
- Error Messages: Rust will provides error messages in detailed if something goes wrong. it has helping you debug easily.
Exercises
Modify the program according to you for print multiple lines:
fn main() {
println!("Hello, Rust!");
println!("Let's learn something new!");
}
Try printing numbers or variables yourself:
fn main() {
let year = 2025;
println!("Welcome to the year {}!", year);
}
Output:
Welcome to the year 2025!
Key Difference Between Macros vs Functions
Feature | Macros (macro_rules!) | Functions (fn) |
---|---|---|
Execution | It is expand at compile time | It is execute at runtime |
Flexibility | It can take variable arguments | Fixed number of arguments |
Metaprogramming | It can generate code | No |
Performance | No function call overhead in the macros | Function call overhead in the functions |