"Hello, World!" with Rust and Cargo.

"Hello, World!" with Rust and Cargo.

Rust is an open source, systems programming language and has been voted "most loved" in the Stack Overflow Developer Survey 2018. In this article, we'll look at "Hello World" with Rust, as well as how we can use Cargo to manage our dependencies.

As always, we'll start off with "Hello World" to establish a baseline for creating a new Rust project and running the binary.

To build our project, we'll need to install Rust on our machine. The easiest way to do this is with rustup, a tool created to easily manage Rust versions and release channels. Run the following in your terminal to get started:

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

From here on out, you'll be prompted with on-screen instructions. Select the default or automatic options as no custom installation options are required for this tutorial.

Now that we have Rust, we can go ahead and make a new Rust project! Type the following in your terminal:

# Create a new directory for our Rust project
mkdir hello_rust

# Create a new Rust file
cd hello_rust && touch hello.rs

# Open this up in your favorite editor
code .

We can then create our very first function:

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

If you've done any programming in the past, this should look relatively familiar to you. Let's look at it in more detail:

①: We can declare a new function with the fn keyword, and we open the function body with an opening bracket.
②: We use the println! macro to output the text "Hello, World" into our console. Note: Notice how I use the word 'macro' here, we'll be looking at macros in more detail in future articles!
③: We close our function with a closing bracket.

We can then run our project by building the binary:

# Build the rust binary
rustc hello.rs

# List files inside of the directory

# You'll notice that we now have a hello.exe on Windows or hello on macOS or Linux. Open it with either:
open hello
open hello.exe

Using Cargo

When developing applications we'll often use third-party dependencies, and managing these dependencies can be painful, depending on the technologies being used. Thankfully, Rust makes this easy with the Cargo package manager!

Cargo allows us to manage dependencies and project metadata, as well as making it easier to build our project with rustc. Cargo should come with every stable Rust build, and as such, we should have access to this already by simply installing Rust. We can check to see if we have Cargo by running the following in the terminal:

cargo --version
cargo 1.30.0 (a1a4ad372 2018-11-02)

New Cargo project

We can use Cargo to create a new Rust project by running the following in the terminal:

# Creates a new Cargo project named hello_rust
cargo new hello_rust

# List the files inside of hello_rust
cd hello_rust && ls

Cargo.toml src

# Open Cargo.toml with your favorite editor
code Cargo.toml

name = "hello_rust"
version = "0.1.0"
authors = ["PaulHalliday <hello@paulhalliday.io>"]


# Change directory to src and open main.rs
cd src && code main.rs

We can see from the above that Cargo has created a new project which includes a Cargo.toml file that contains information about our project. Any new dependencies that are installed will be noted here.

Here's an example of installing a new dependency such as log to our Cargo.toml:

name = "hello_rust"
version = "0.1.0"
authors = ["PaulHalliday <hello@paulhalliday.io>"]

log = "0.4"

You'll also notice a familiar set of code inside of main.rs, our "Hello, World" example from earlier:

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

If we type cargo run into our terminal, you'll notice that it builds and executes our Rust binary to display "Hello, world!"  in the console.