Skip to content

Beginner’s Guide to Rust: How to Print Hello World

Write the “Hello, World!” Program
Create a new Rust file, e.g., hello.rs.

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


 fn
main() is the entry point of every Rust program.
• println! is a macro (note the !) used for printing text to the console.

Compile and Run the Program
Use the following commands in your terminal
Compile the Program(Terminal)

User>rustc hello.rs

This generates an executable file named hello (or hello.exe on Windows).
Run the Executable

User>./hello


Output

User>Hello, World!

Cargo (Optional, Recommended)

Cargo is Rust’s package manager and build system. You can create and run projects more efficiently with it.
Create a New Project (Open Terminal)

User>cargo new hello_world 
User>cd hello_world

‘code .’ press enter open vs code

Edit the src/main.rs File Cargo creates a default “Hello, World!” program in src/main.rs:

open main.rs

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

Build and Run the Program

User>cargo run

Output

User>Hello, World!

That’s it! You’ve written and executed your first Rust program. 🎉

Leave a Reply

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