Your First Rust Program: Hello, World!
Welcome to the exciting world of Rust! Our journey begins with the quintessential "Hello, World!" program. This simple program serves as your first interaction with the Rust compiler and runtime, laying the groundwork for more complex applications.
Setting Up Your Rust Environment
Before you can write and run Rust code, you need to install Rust. The recommended way to do this is by using
rustup
rustup
Creating Your First Rust Project
Rust projects are typically managed using Cargo, Rust's build system and package manager. Cargo handles tasks like creating new projects, compiling code, and managing dependencies. To create a new project, open your terminal or command prompt, navigate to the directory where you want to create your project, and run the following command:
cargo new hello_world
This command creates a new directory named
hello_world
hello_world/├── Cargo.toml└── src/└── main.rs
Cargo.toml
src/main.rs
Writing the "Hello, World!" Code
Open the
src/main.rs
fn main() {println!("Hello, world!");}
Let's break this down:
`fn main()` defines the main function, the entry point of every executable Rust program.
The fn
keyword declares a function. main
is the special name for the function that runs first. Parentheses ()
indicate that this function takes no parameters. Curly braces {}
enclose the body of the function.
In Rust, functions are declared using the fn
keyword. The main
function is special because it's the starting point for all executable Rust programs. It doesn't take any arguments and doesn't return any values by default. The code that the function executes is placed within its curly braces {}
.
`println!` is a Rust macro that prints text to the console.
The println!
macro is used to output text. The exclamation mark !
signifies that it's a macro, not a regular function. The text to be printed is enclosed in double quotes ""
.
The println!
macro is a powerful tool for displaying output. The !
symbol is crucial; it indicates that you are calling a macro. Macros in Rust are a way of writing code that writes other code (metaprogramming). The string literal "Hello, world!"
is the message that will be printed to the standard output (your console). Macros often have a trailing !
to distinguish them from functions.
!
after println
signify?It indicates that println
is a macro, not a regular function.
Compiling and Running Your Program
Now that you've written your code, it's time to compile and run it. Navigate back to the root directory of your project (
hello_world/
cargo run
Cargo will first compile your code (if it hasn't been compiled already) and then run the resulting executable. You should see the following output:
Hello, world!
You can also compile your code without running it using cargo build
. The executable will be placed in the target/debug/
directory.
Understanding Ownership (A Glimpse)
While "Hello, World!" is simple, it's important to know that Rust's core strength lies in its memory safety guarantees, primarily achieved through its ownership system. This system ensures memory safety without a garbage collector. We'll delve deeper into ownership in subsequent modules, but for now, understand that every value in Rust has a variable that's called its owner. There can only be one owner at a time, and when the owner goes out of scope, the value will be dropped.
The println!
macro is a fundamental tool for interacting with the user by displaying output to the console. It's a macro, indicated by the !
symbol, which means it's a piece of code that generates other code at compile time. This allows for more flexibility than regular functions. The macro takes a format string as its first argument, which can include placeholders for other values that you want to print. In our case, it's a simple string literal.
Text-based content
Library pages focus on text content
Learning Resources
The official Rust book provides a clear and concise introduction to writing and running your first Rust program.
This page guides you through installing Rust and the `rustup` toolchain manager on various operating systems.
Learn the basics of Cargo, Rust's build system and package manager, including creating new projects.
An in-depth look at Rust macros, explaining what they are and how they differ from functions.
A practical, hands-on approach to learning Rust through runnable examples, starting with the classic 'Hello, World!'.
A beginner-friendly video tutorial that walks through setting up Rust and writing a 'Hello, World!' program.
An introductory video explaining the core concepts of Rust's ownership, borrowing, and lifetimes.
An overview of Rust's features, philosophy, and why it's gaining popularity for systems programming.
A comprehensive Wikipedia article covering Rust's history, features, and ecosystem.
A blog post that breaks down the fundamental concept of ownership in Rust with clear examples.