Learning a new programming language often starts with a simple program that prints "Hello, world!" to the screen. Now that you've installed Rust, let's write your first Rust program!
Creating a Project Directory
Start by creating a directory to store your Rust code. Rust doesn't impose any specific requirements on where your code should reside.
Open a terminal and enter the following commands to create a `projects` directory and a `hello_world` directory within it:
For Linux, macOS, and PowerShell on Windows:
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
For Windows CMD:
> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir hello_world
> cd hello_world
Writing and Running a Rust Program
Next, create a new source file named `main.rs`. Rust files always end with the `.rs` extension. If your filename consists of more than one word, use an underscore to separate them. For example, use `hello_world.rs` rather than `helloworld.rs`.
Open the `main.rs` file you just created and enter the following code:
Rust
fn main() {
println!("Hello, world!");
}
Save the file and return to your terminal window in the `~/projects/hello_world` directory. On Linux or macOS, enter the following commands to compile and run the file:
$ rustc main.rs
$ ./main
You should see `Hello, world!` printed to the terminal. On Windows, enter the command `.\main.exe` instead of `./main`:
> rustc main.rs
> .\main.exe
Regardless of your operating system, you should see `Hello, world!` printed to the terminal. If you don't see this output, refer back to the Troubleshooting section of the Installation chapter for help.
If `Hello, world!` did print, congratulations! You've officially written a Rust program. That makes you a Rust programmer—welcome!