Rust Introduction & Setup
Estimated reading: 3 minutes 34 views

🦀 Rust – Hello World Example: Your First Rust Program Explained

🧲 Introduction – Why Start with Hello World?

The best way to begin learning any programming language is by writing a simple “Hello, World!” program. In Rust, this not only teaches you the basic syntax but also introduces core concepts like functions, macros, and compilation flow.

🎯 In this guide, you’ll learn:

  • How to write, compile, and run your first Rust program
  • The role of the main function and println! macro
  • Project structure using cargo
  • How Rust’s compilation produces executables

📝 Writing a Basic Hello World Program

Here’s the simplest Rust program you can write:

fn main() {
    println!("Hello, Rustaceans!");
}

🔍 Explanation Line-by-Line:

CodeMeaning
fn main()Defines the entry point of the program (like main() in C/C++)
{}Denotes the body of the function
println!("...")A macro that prints to standard output
! after printlnIndicates it’s a macro, not a function

📤 Output:

Hello, Rustaceans!

🔧 Compile and Run with rustc

If you’re using raw Rust tools without cargo, save the code to a file named hello.rs.

📦 Step-by-step (manual):

rustc hello.rs
./hello      # or hello.exe on Windows

✅ This creates an executable named hello that you can run directly.


🚀 Creating a Hello World Project with Cargo

cargo is the preferred way to manage Rust projects.

🛠️ Step 1: Create a new project

cargo new hello_project
cd hello_project

This generates:

hello_project/
├── Cargo.toml       # Project metadata & dependencies
└── src/
    └── main.rs      # Default entry point

📜 Step 2: Code in src/main.rs

fn main() {
    println!("Hello from Cargo!");
}

▶️ Step 3: Run the program

cargo run

📤 Output:

   Compiling hello_project v0.1.0
    Finished dev [unoptimized + debuginfo] target(s)
     Running `target/debug/hello_project`
Hello from Cargo!

📚 println! Macro Variations

fn main() {
    println!("Rust");                          // Basic string
    println!("{} {}", "Hello", "Rust");        // Format with {}
    println!("Sum is: {}", 5 + 10);            // Expression output
}

📤 Output:

Rust
Hello Rust
Sum is: 15

⚠️ Common Beginner Mistakes

MistakeError Message / IssueFix
Missing ! in printlnexpected function, found macroUse println! not println
Misspelling maincan’t find function MainAlways use lowercase fn main()
Forgetting semicolonunexpected tokenEnd expressions with ; when needed

🧪 Run Rust Code Online

Try Rust in your browser without installing anything:

🔗 https://play.rust-lang.org

Paste your Hello World code, hit Run, and see the output instantly.


📌 Summary – Recap & Next Steps

Your first Rust program is more than just a greeting—it teaches you syntax, compilation, and core structure. From here, you’re ready to explore variables, data types, and control flow.

🔍 Key Takeaways:

  • main() is the starting point for Rust programs
  • println! is a macro used to print messages to the console
  • Use cargo for real-world projects and easier workflow
  • Rust compiles to native executables

⚙️ Move on to learning variables, constants, and data types in Rust – Variables.


❓FAQs


Why does Rust use println! instead of print()?
✅ Rust uses macros (like println!) for compile-time code generation. The ! denotes a macro, not a regular function.


Can I run Rust code without installing Rust?
✅ Yes, use play.rust-lang.org or TutorialsPoint Rust compiler for online execution.


Do I need cargo to write Rust programs?
✅ No, but cargo simplifies project creation, building, and dependency management. It’s recommended for all but the smallest examples.


What does rustc do?
rustc is the Rust compiler. It compiles .rs source files into native executables.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Rust – Hello World Example

Or Copy Link

CONTENTS
Scroll to Top