Getting Started with Rust (Hello World, Mac)
In this article, I'm going to show you how to install the tools to create and run a Hello World app using Rust.
These instructions were written and tested on a Mac.
Step 1. Install rustc and cargo
The first thing you should do is see if you have the necessary tools installed already.
- Open the Terminal application on your Mac
You can find it in the Applications > Utilities folder.
- Check if you have rustc and cargo installed by typing the following commands in the Terminal:
rustc --version
cargo --version
If rustc and cargo are not installed:
On a Mac, you can install them using brew with this command:
brew install rustup
You may see this message at the end of the install:
Please run `rustup-init` to initialize `rustup` and install other Rust components.
- Run it:
rustup-init
- Select the default option
When the install finishes, follow the instructions to source your shell and update your path (or just close the terminal window and open a new one).
For example (note the starting dot):
. "$HOME/.cargo/env"
- Verify everything was installed:
rustc --version
cargo --version
Step 2. Create a Rust projects root folder
In a terminal window, run the following commands:
mkdir -p ~/projects/rust/
cd ~/projects/rust/
Step 3. Create a Rust project
In Rust, projects are managed using Cargo, which automates tasks such as dependency management, building, and testing.
- Make sure you are in the root of your Rust projects folder:
cd ~/projects/rust/
- Run cargo to create a new project:
cargo new rust-101
The command should have created a new directory named rust-101
It should contain a basic Rust project structure.
- Switch to the folder and view the contents:
cd rust-101
ls -ls
If you prefer to create the folder first, you could have done something like this instead:
mkdir -p ~/projects/rust/rust-altway
cd ~/projects/rust/rust-altway
cargo init .
ls -ls
The result would have been the same.
Step 4. Review the generated code
The new project should already contain a default program.
- Open src/main.rs and verify that it looks like this:
fn main() {
println!("Hello, world!");
}
Feel free to change the message.
- The default code defines a
main
function that prints "Hello, world!" to the console.
Step 5. Build the program
You can build the program to run using cargo.
- From the root of your project folder, run the following:
cargo build
The cargo build command will compile your Rust code and create an executable binary in the target/debug
directory.
ls -ls target/debug
Step 6. Run the program
You can run the binary directly like this:
target/debug/rust-101
You can run it with a bit more debug info, using cargo like this:
cargo run
Or more quietly using cargo like this:
cargo run --quiet
Congratulations! You've successfully created, built, and run your first Rust program!
Now let's add some tests to make sure that our code runs as expected.
Step 7. Testing
Rust has a built-in testing framework that makes it easy to write and run tests for your code. Follow these steps to add tests to your "Hello, World!" program:
- Open the
src/main.rs
file in your editor - Add the following test function below the
main
function and save the file:
#[cfg(test)]
mod tests {
use std::process::Command;
#[test]
fn test_hello_world_output() {
let output = Command::new("cargo")
.arg("run")
.arg("--quiet")
.output()
.expect("Failed to run command");
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");
let stderr = String::from_utf8(output.stderr).expect("Invalid UTF-8 in stderr");
assert!(output.status.success(), "Command failed with: {}", stderr);
assert_eq!(stdout.trim(), "Hello, world!");
}
}
This test function checks that the main
function prints "Hello, world!" to the console as expected. Edit the expectation if you changed the default message.
Step 8. Run the test
To run the tests, you can use cargo again.
- From the root of your project, run this command:
cargo test
Cargo will compile your code and execute the test function.
If everything is working correctly, you should see a message indicating that the one test passed.
Example
You can find the code for this article here:
Conclusion
In this article, you learned how to:
- Install the tools for working with Rust on a Mac
- Create, build, and test a Rust "Hello, World!" program
References
- rust-lang.org
- Support for Apple M1 devices
- rust-analyzer (VS Code Extension)
- https://rustup.rs - alternate ways to install
- https://crates.io - Rust package registry
- The Cargo Book