Rust: Hello, World! - Getting Started
This guide will walk you through setting up a Rust development environment and writing your first "Hello, World!" program.
1. Installation
Before you can write Rust code, you need to install the Rust toolchain. The recommended way is to use rustup.
- Linux/macOS: Open a terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the on-screen instructions. Typically, you'll just press 1 to proceed with the default installation.
- Windows: Download and run
rustup-init.exefrom https://www.rust-lang.org/tools/install. Follow the on-screen instructions.
After installation, close and reopen your terminal (or restart your shell) to ensure the Rust tools are added to your PATH.
2. Verify Installation
To confirm Rust is installed correctly, open a terminal and run:
rustc --version
cargo --version
You should see output similar to this (the versions will vary):
rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-12-11)
3. Create a New Project
Rust uses a package manager and build tool called Cargo. Let's create a new project:
cargo new hello_world
cd hello_world
This command does the following:
cargo new hello_world: Creates a new directory namedhello_worldwith a basic Rust project structure.cd hello_world: Changes the current directory to the newly created project directory.
4. Project Structure
Inside the hello_world directory, you'll find:
Cargo.toml: This is the project's manifest file. It contains metadata about your project, such as its name, version, and dependencies.src/main.rs: This is the source code file where your main program logic will reside.
5. Write the "Hello, World!" Code
Open src/main.rs in a text editor. You'll find some default code already there. Replace it with the following:
fn main() {
println!("Hello, World!");
}
Let's break down this code:
fn main() { ... }: This defines themainfunction, which is the entry point of your Rust program. Execution always begins here.println!("Hello, World!");: This line prints the text "Hello, World!" to the console.println!is a macro that handles formatted output. The!indicates it's a macro, not a function.
6. Build and Run the Program
Now, let's build and run your program:
cargo run
This command does the following:
cargo build: Compiles your Rust code into an executable.cargo run: Executes the compiled executable.
You should see the following output in your terminal:
Compiling hello_world v0.1.0 (/path/to/hello_world)
Finished dev [unoptimized + debug] target(s) in 0.50s
Running `target/debug/hello_world`
Hello, World!
Congratulations! You've successfully written and run your first Rust program.
7. Understanding Cargo.toml
Open Cargo.toml. You'll see something like this:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[package]: This section defines the package metadata.name: The name of your project.version: The version number of your project.edition: The Rust edition you're using.2021is the latest as of this writing.[dependencies]: This section is where you'll list any external libraries (crates) that your project depends on. For this simple program, we don't have any dependencies.
This "Hello, World!" example is just the beginning. Rust is a powerful and versatile language with a lot to offer. Explore the official Rust documentation (https://doc.rust-lang.org/) to learn more.