LibraryYour First Rust Program: "Hello, World!"

Your First Rust Program: "Hello, World!"

Learn about Your First Rust Program: "Hello, World!" as part of Rust Systems Programming

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

code
rustup
, the Rust toolchain installer. It allows you to manage different Rust versions and associated tools.

What is the recommended tool for installing and managing Rust?

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:

code
cargo new hello_world

This command creates a new directory named

code
hello_world
with the following structure:

code
hello_world/
├── Cargo.toml
└── src/
└── main.rs

code
Cargo.toml
is the manifest file for your project, containing metadata and dependencies.
code
src/main.rs
is where your Rust source code will reside.

Writing the "Hello, World!" Code

Open the

code
src/main.rs
file in your favorite text editor. You'll find some boilerplate code already generated by Cargo. Replace it with the following:

rust
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.

What does the ! 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 (

code
hello_world/
) in your terminal and use Cargo:

code
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:

code
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 Rust Programming Language - "Hello, world!"(documentation)

The official Rust book provides a clear and concise introduction to writing and running your first Rust program.

Rust Installation Guide(documentation)

This page guides you through installing Rust and the `rustup` toolchain manager on various operating systems.

Cargo Book - Getting Started(documentation)

Learn the basics of Cargo, Rust's build system and package manager, including creating new projects.

Rust Macros Explained(documentation)

An in-depth look at Rust macros, explaining what they are and how they differ from functions.

Rust by Example - Hello, World(tutorial)

A practical, hands-on approach to learning Rust through runnable examples, starting with the classic 'Hello, World!'.

Introduction to Rust Programming(video)

A beginner-friendly video tutorial that walks through setting up Rust and writing a 'Hello, World!' program.

Understanding Rust's Ownership System(video)

An introductory video explaining the core concepts of Rust's ownership, borrowing, and lifetimes.

What is Rust?(blog)

An overview of Rust's features, philosophy, and why it's gaining popularity for systems programming.

Rust Programming Language(wikipedia)

A comprehensive Wikipedia article covering Rust's history, features, and ecosystem.

Rust Ownership: The Core Concept(blog)

A blog post that breaks down the fundamental concept of ownership in Rust with clear examples.