Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.
With Rust installed and your development environment set up, you can write your first Rust program.
For our first program, we'll use the Rust compiler (rustc) directly, even though this isn't the recommended way to build projects. We'll do it just once to understand how the compiler works. For all future projects, we'll use Cargo, which compiles your whole project for you.
Open your preferred text editor or IDE and create a new file named hello.rs. Enter the following code:
fn main() {
println!("Hello, Rust!");
}
This simple program has a single function named main, which prints the message "Hello, Rust!" to the console using the println! macro. Like in other languages, main is the entry point of every Rust program.
Note: In Rust, a macro is code that generates other code (metaprogramming). The println! macro prints text to the console, similar to PHP:
- Rust:
println!("Hello, Rust!"); - PHP:
echo "Hello, Rust!";
What's next
Next: Compiling and running your Hello Rust.
From PHP to Rust — Getting Started with Rust