First Program with Rust
By Charles LAZIOSI
- Published on

Sharing
To write a "Hello, world!" program in Rust, you will need to go through a few steps if you haven't already set up your Rust environment:
- Install Rust: First, you need to install Rust. The easiest way to do this is by using
rustup
, which is the official installer for the stable installation of the Rust programming language. You can install it by following the instructions at https://www.rust-lang.org/tools/install.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Create a New Project: Once Rust is installed, you can create a new project using Cargo, which is Rust's package manager and build system. To create a new project, open your terminal or command prompt and type:
cargo new hello_world
This will create a new directory called hello_world
with the following contents:
Cargo.toml
– a configuration file for Cargo.src
– a directory containing your source code.src/main.rs
– the main source file where your code will go.
- Write Your "Hello, World!" Program: Open
src/main.rs
in your favorite text editor and write the following code:
fn main() {
println!("Hello, world!");
}
Here's what this program does:
fn main()
declares the main function (the entry point of the program).{}
indicate that this is a compound statement (a block of code).println!
is a macro that prints text to the console followed by a newline.
- Build and Run Your Program: Go back to your terminal or command prompt, navigate to your project directory (
hello_world
) and run the following command:
cargo run
Cargo will compile your code and then run the resulting executable. You should see output like this:
Compiling hello_world v0.1.0 (/path/to/your/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.5 secs
Running `target/debug/hello_world`
Hello, world!
Congratulations! You've just written and run your first "Hello, world!" program in Rust.
Remember that every time you make changes to your source code files, you'll need to build and run again with cargo run
. If you just want to compile without running, use cargo build
.