Getting Started
In this page, we'll write a Hello World! program and explore cargo, which is Rust's package manager and build system.
Hello World
To get started with a Rust program, you'll need to create a project directory and a source file with .rs extension. Rust files always end with this extension.
Project Structure
Current project structure:
hello_world
└── main.rsMain Code
Now, add the following code inside the main.rs file:
fn main() {
println!("Hello World!");
}Compile Code
To compile the code, use the following command (inside the project directory):
rustc main.rsThis should create a new file called main in the same directory.
Run Code
To run the code, simply execute the newly created file as:
./main
# prints Hello World! to consoleBreaking Down the Code
NOTE
In Rust, the standard style is to indent using four spaces instead of a tab.
- The function is named
main, which is a special entry point in every executable Rust program. It’s always the first function that runs when your program starts. - The function body is enclosed within curly braces
{}. - The
println!line calls a Rust macro. The exclamation mark (!) indicates that it's calling a macro and not a regular function. (You'll learn about the differences between macros and functions later) - The string to be printed is passed to the macro in the same way arguments are passed to a function.
- Each statement ends with a semicolon (
;), marking the end of that line of code.
Format Code
To maintain a consistent coding style across Rust projects, you can use the built-in formatter tool rustfmt. It automatically formats your code according to the official Rust style guidelines. The Rust team includes this tool with the standard Rust installation, so it should already be available on your system.
IDE Support
Many modern IDEs and editors now offer excellent support for Rust. The Rust team has been focussing on enabling great IDE support via rust-analyzer.
rust-analyzer is the main language server for Rust. It provides real-time analysis and assistance as you write Rust code, including:
- Autocomplete for functions, variables, and modules
- Go to definition / Find references
- Hover tooltips with type information and documentation
- Inline error checking and diagnostics
- Code formatting and refactoring tools
- Inlay hints (showing types and parameter names inline)
It works using the Language Server Protocol (LSP), a standard that allows editors and IDEs to communicate with language tools. In this setup, rust-analyzer serves as the brain that understands your code, while your editor provides the interface you interact with.
Comparing with Dynamic Languages
Unlike dynamic languages such as Ruby, Python, or JavaScript, Rust separates the steps of compiling and running a program. Rust is an ahead-of-time compiled language, which means you can compile your code into an executable and share it with others, and they can run it even without having Rust installed.
In contrast, if you share a .rb, .py, or .js file, the recipient must have the corresponding language interpreter installed to run it. However, in those languages, you typically compile and run your code with a single command.
Each approach has its advantages and trade-offs. Rust prioritizes performance and reliability through compilation, while dynamic languages emphasize flexibility and ease of use.
Cargo
Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building/compiling your code, downloading the external libraries your code depends on, and building those libraries. These external libraries are referred to as your project’s dependencies.
Cargo comes installed with Rust if you use the official installers. To confirm, use:
cargo --versionCreating a Project
To create a new project using Cargo, use:
cargo new hello_worldThis is how the project structure looks like:
hello_world
├── Cargo.toml
└── src
└── main.rsThis is very similar to the project you created earlier, except that this has an additional directory and a .toml file. Cargo also initialized a new Git repository along with a .gitignore file.
Cargo expects all your source files to live inside the src directory. The top-level directory is just for README files, license information, configuration files, and anything else not related to your code.
The Cargo configuration file uses TOML (Tom's Obvious, Minimal Language) format. It contains all the configuration information needed by Cargo to compile your program.
Cargo.toml
NOTE
In Rust, packages of code is referred to as crates.
Example:
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024"
[dependencies]The [package] section contains the info about the package itself: the name, the version, and the edition of Rust to use.
The [dependencies] section contains all your project dependencies.
Building and Running a Cargo Project
NOTE
The default build is a debug (development) build.
- Within the project root directory, run this command to build your project:
cargo buildThis command creates an executable file in target/debug/hello_world.
- You can run the executable with this command:
./target/debug/hello_world
# prints Hello World! to consoleNOTE
Running cargo build for the first time also causes Cargo to create a new file at the top level: Cargo.lock. This file keeps track of the exact versions of dependencies in your project.
- You can also compile the code and run the resultant executable all in one command:
cargo runThis command skips compilation step if there is no change in the source code.
- Cargo also provides a command to quickly check if your code compiles without producing an executable:
cargo checkThis command is useful to run periodically as you write code to ensure everything still compiles. It’s also much faster than cargo build because it skips the step of generating the final executable.
Building for Release
When your project is finally ready for release, you can use cargo build --release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.
The optimizations make your Rust code run faster, but turning them on lengthens the time it takes for your program to compile. That’s why Rust provides two build profiles:
- Development (
cargo build) - optimized for quick, frequent rebuilds. - Release (
cargo build --release) - optimized for maximum runtime performance.
If you’re benchmarking your program’s performance, always build with --release and test the executable in target/release.
