Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Hello World

Once you have installed the Rust development environment on your computer, We can explain the process of creating a Rust project, writing a simple “Hello, World!” program, compiling it, and running it.

Cargo

Cargo is Rust’s build system and package manager. It allows you to:

If you come from Python, and are familiar with pip or conda or other environments to manage your dependencies, you’ll find Cargo extremely useful. If you come from a JVM-based language such as Java or Scala, you’ll find Cargo does much of what Maven/Gradle/Ant does for JVM languages, an all-in-one environment. And, if you come from C or C++ you may enjoy not having to deal with Makefile anymore :)

To make sure you have cargo installed, enter the following command into the terminal: cargo --version

Creating a Project (crate) with Cargo

To create a project with cargo, run the following commands, which create a crate. A crate is a package of code that can either be a binary (executable) or a library.

cargo new hello_cargo
cd hello_cargo

In this example, we will be working with a new (binary) crate named hello_cargo. When you cd into the directory, you should see that Cargo has generated two files:

Open Cargo.toml in a text editor. You should see the configuration information for the package, and the information that Cargo needs to compile your program. Make sure your name and email are correct.

Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Name <email@uchicago.edu>"]
edition = "2021"

[dependencies]

Next, open src/main.rs. You should see that Cargo has generated a Hello World program for you.

src/main.rs
fn main() {
    println!("Hello, world!");
}

Building and Running a Cargo Project

Building a crate in a workspace

For our project, we are using a workspace, which is a collection of related crates. When code repositories grow large, workspaces are a great way of organizing code to make reading and modifying it easier. If you want to execute the build, test, run, etc. for a particular crate you add -p <crate_name> after the command. For example: cargo test -p memstore would just compile and execute the tests in the memstore crate (you’ll soon what that is in the context of CrustyDB). If memstore depends on other crates (external or in the workspace), it would build them first.

Read more about Cargo in the Rust Book.