Skip to content

Rust - HelloWorld Example

The best way to start learning a new language is by writing a small program. Let’s create the classic “Hello, World!” program using modern Rust development tools.

While you can compile Rust files manually with rustc, the standard and highly recommended approach is to use Cargo, Rust’s package manager and build tool. Cargo handles project creation, compilation, dependency management, and much more.

Open your terminal or command prompt and run the following command:

cargo new hello_world

Cargo will create a new directory called hello_world with the following structure:

hello_world/
├── Cargo.toml
└── src/
└── main.rs
  • Cargo.toml: The manifest file for your project. It contains metadata like the project name, version, and dependencies.
  • src/main.rs: The source code file where you’ll write your program.

Open the src/main.rs file. Cargo has already generated a simple “Hello, world!” program for you:

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

Let’s break this down:

  • fn main() { ... }: This defines a function named main. The main function is special: it’s always the first code that runs in every executable Rust program.
  • println!: This is a Rust macro. Macros are a way of writing code that writes other code (meta-programming). The ! signifies that you’re calling a macro instead of a normal function.
  • "Hello, world!": This is a string literal, a piece of text passed as an argument to the println! macro.
  • ;: The line ends with a semicolon, which indicates that this expression is over and the next one may begin.

Navigate into your new project directory in the terminal:

cd hello_world

Now, use Cargo to compile and run the program with a single command:

cargo run

You should see the following output in your terminal:

Compiling hello_world v0.1.0 (/path/to/your/project/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/hello_world`
Hello, world!

Congratulations! You have successfully written and executed your first Rust program using the modern toolchain.

Good code is well-documented. Comments are notes that are ignored by the compiler but are invaluable for other developers (and your future self). Rust supports two main types of comments:

fn main() {
// This is a single-line comment. It explains the next line of code.
println!("Comments make code easier to understand.");
/*
This is a multi-line comment.
It can span several lines and is useful for longer explanations.
*/
let _x = 5; // We can even put comments at the end of a line.
}

For the best development experience, we recommend using a modern code editor with Rust support:

  • Visual Studio Code with the rust-analyzer extension is the most popular choice. It provides real-time feedback, code completion, and error checking.
  • Other editors like those from JetBrains (IntelliJ with the Rust plugin, CLion) also offer excellent support.

Additionally, many online platforms like the Rust Playground allow you to write and run Rust code directly in your browser, which is great for experimenting with small snippets.