Practical Rust: Installation & Hello World for Mac OS & Linux

Recently, a new language called Rust has come into the spotlight, aiming to be a ‘blazingly fast’ language meant for systems programming, with an emphasis on safety. Essentially, it aims to fix many of the common problems with current systems languages such as C++ and C, while maintaining their performance. In my experience with the language, I can tell you that Rust certainly addresses safety in a very creative way, using an ultra strict compiler and well defined rules for programming. However, with this additional safety and requirement of well written code, Rust can be extremely difficult for individuals who are newer to the language, and just starting out. It is the goal of this series to explain Rust in a very practical and usable way, while demonstrating multiple ways certain language features can be used to create software with additional security and lower runtime.


Installation

The people behind the Rust programming language (Mozilla) have made it very easy for individuals to install and setup the language for their computer, regardless of system type. To install Rust, simply enter the following command in Terminal (or Command Prompt):

curl https://sh.rustup.rs -sSf | sh

After downloading the installer, the script will display the following three options to you:

Rust installation options

For most users, proceeding with installation (1) will be the best option. This will install the compiler, and the project management tool for rust applications, Cargo. After installation, you may be prompted to enter a command to configure the current command line shell for cargo.


Hello World Project

Once Rust is successfully installed, it is customary to create a Hello World application to verify the installation was successful. The project management tool provided by Rust called Cargo, makes it easy to create new projects and manage dependencies. The addition of Cargo is arguably one of the best features of the Rust ecosystem.

To create a new application, perform the following command in the directory of your choice (substituting <APP_NAME> with the name of your app):

cargo new --bin <APP_NAME>

From here, the tool will create a new folder which looks like this:

├── Cargo.toml
└── src
└── main.rs

The Cargo.toml file will be auto-generated by Cargo, and will contain some simple package information, along with a list of dependencies. The main.rs file will be auto-populated with a simple hello world application.

src/main.rs file:

fn main() {
println!("Hello, world!");
}

The anatomy of a rust program is very simple, and similar to languages like C/C++ in structure, using a main() function to initialize the software.

Now that everything is setup, lets run the application. You can use the Cargo tool to build & run the application by executing the following command in the project folder created by Cargo:

cargo run

If everything was successful, you should see a screen like this:

The ideal output of a Hello World program in Rust

Conclusion

In this tutorial, we have installed Rust and it’s project management tool, Cargo. Additionally, we have created a Hello World program to print “Hello, world!” to the screen, as customary with learning a new language. In my opinion, Rust has fixed many of the shortcomings of languages like C/C++, and through further refinements to the compiler, could eventually match or exceed the speed of mature languages.

The next tutorial will cover object types and simple control flow in Rust, and allow us to create a simple — yet practical — command line application to perform a task from user input.

If you would like to get notified about when the next tutorial comes out, or get in contact with me, be sure to follow my twitter @gallantmrgn. Thanks!