Summary: in this tutorial, you’ll learn how to write the first Rust program called Hello, World!
Developing the Rust Hello World program #
You can follow these steps to develop the Hello World program in Rust:
Step 1: Create a New Rust Project Folder #
- First, open your command line tool (Command Prompt on Windows, Terminal on macOS/Linux).
- Second, navigate to a directory where you want to save your Rust projects. For example:
cd Documents
mkdir rust_projects
cd rust_projectsCode language: Rust (rust)- Third, create a new folder for your Hello World program:
mkdir hello_world
cd hello_worldCode language: Rust (rust)Step 2: Write the Rust Code in an Editor #
- Open your favorite text editor (e.g., VS Code, Sublime Text, or Notepad++).
- Inside the
hello_worldfolder, create a file namedmain.rs. - Write the following Rust code:
fn main() {
println!("Hello, world!");
}Code language: Rust (rust)Step 3: Compile the Program Using rustc #
You need to compile Rust code before running. To do that you use the Rust compiler (rustc):
- In your command line, ensure you are inside the
hello_worldfolder. - Run the following command to compile:
rustc main.rs - This will create an executable file in the same folder.
- On Windows, the file will be named
main.exe. - On macOS/Linux, the file will be named
main.
- On Windows, the file will be named
Step 4: Run the Program #
Now, run the executable file you just compiled:
- On Windows:
main.exe - On macOS/Linux:
./main
You should see this output:
Hello, world!Code language: Rust (rust)How it works #
fn main() { ... }Code language: Rust (rust)fndeclares a function.mainis the entry point of every Rust program. When your program starts, Rust looks for and executesmainfirst.()meansmaintakes no parameters.- The curly braces
{ ... }enclose the function body (the block of code that runs).
println!("Hello, world!");Code language: Rust (rust)println!is a macro (not a regular function). The!indicates macro invocation. Macros generate code at compile time. For basic use, treatprintln!like a print function that adds a newline."Hello, world!"is a string literal of type&str. The double quotes define a string literal.- The semicolon
;ends the statement. Rust use semicolon to separate lines of code.
Common Mistakes and How to Fix Them #
Here are some common mistakes you may encounter when writing the first Rust program:
Forgetting the ! in println! #
- Wrong:
println("Hello"); - Right:
println!("Hello");
The println! is a macro; it must have !.
Using curly quotes instead of straight quotes #
- Wrong:
println!(“Hello”); - Right:
println!("Hello");
Only straight ASCII quotes " are valid string delimiters.
Missing semicolon #
- Often harmless after a macro call in
main, but as a rule of thumb, end statements with;.
Mismatched braces #
- Ensure every
{has a matching}.
Using the Rust Playground Online #
The Rust Playground is an official browser-based editor that allows you to write, run, and share Rust code without installing anything on your computer.
This means you don’t need to download or install anything to start experimenting with Rust. It’s perfect if you want to try out Rust quickly or test snippets of code.
Why Use the Playground? #
- No Installation Required: Great for trying Rust instantly.
- Shareable Links: Easily share your code with others by sending a unique URL.
- Safe Environment: Run code in a sandbox without worrying about affecting your computer.
- Great for Learning: Test small code snippets while studying tutorials or books.
How to Access the Playground #
- Open your web browser and go to: https://play.rust-lang.org.
- You will see a text editor where you can write your Rust code.
- And you’ll see the buttons to Run, Format, and Share your code.
Key Features #
- Run Button: Compiles and executes your code instantly.
- Share Button: Creates a shareable link to your code for forums, chat, or collaboration.
- Format Button: Automatically formats your code according to Rust’s style guidelines.
- Tools Menu: Allows you to switch between stable, beta, or nightly Rust versions.
Writing Your First Code in the Playground #
- First, in the top bar, keep “Channel” as Stable and “Mode” as Debug (both are fine for this exercise).
- Second, enter the following code:
fn main() {
println!("Hello, Rust Playground!");
}
Code language: Rust (rust)- Third, click the Run button. You will see the output appear below the editor.
When Should You Use the Playground? #
- When you are learning Rust basics.
- When you want to test short code snippets before adding them to a project.
- When asking for help on Rust forums or communities, since you can share links.
- When you don’t want to set up a Rust environment yet.
Limitations of the Playground #
- Not designed for large projects.
- Cannot interact with files or your local system.
- Limited to short, experimental programs.
What you’ve learned #
- How to write the minimal Rust program.
- Why
mainis the entry point. - How to print to standard output with
println!. - Basics of statements, string literals, and the semicolon.