Rantings from a guy with way too much free time

I'm Back!!! Diving into Rust!

2021-04-26 programming Rob Baruch

Noobie to Rust? Let’s get started!!

So you want to get into rust? Sure, why not. Let’s do it.

The setup

Assuming you’re on a Mac, let’s get rust up and running so you’re ready to rip into learning the language. We’ll use rustup to install rust. This is the best way to manage your rust environment and toolchain. Not only can you manage toolchains using rustup, but also add-in components to make the experience a lot more enjoyable. More on that later. Not only will rustup install rust for you, but it will also be useful for maintaining the latest versions of rust. Rust has several toolchains, and using rustup will make maintaining them a whole lot easier.


Using rustup, we can add additional components to our environment using rustup component add. Some of the popular components include:

  • rustfmt — Rustfmt is a tool for automatically formatting code.
  • rls — RLS is a language server that provides support for editors and IDEs.
  • clippy — Clippy is a lint tool that provides extra checks for common mistakes and stylistic choices.
  • cargo — Cargo is a package manager and build tool.

To see what components are currently installed, try using a command like this: rustup component list | grep installed | awk -F\- ' { print $1 } ' | uniq

This will list the generic versions of the component. By generic, I mean the name of the component without the full name for the toolchain target actually installed. Drop the awk script, and you’ll see the full name of each component like the following on my laptop:

cargo-x86_64-apple-darwin (installed)
clippy-x86_64-apple-darwin (installed)
rls-x86_64-apple-darwin (installed)
rust-analysis-x86_64-apple-darwin (installed)
rust-docs-x86_64-apple-darwin (installed)
rust-src (installed)
rust-std-x86_64-apple-darwin (installed)
rustc-x86_64-apple-darwin (installed)
rustfmt-x86_64-apple-darwin (installed)


Using rust means we need to use the cargo package manager to iteract with our build environment. Creating new rust projects, running them, installing packages for them, benchmarking them, testing them, and a whole lot more - all happens through cargo. You can read more about cargo. There’s a lot going on there! So it’s worthwhile to peruse the documentation.

Hello World! rusticly speaking

So now that we’ve installed the rust build system, let’s jump right in and see if we can create the classic, hello world! program in rust!

$ cargo new --bin hello_world
     Created binary (application) `hello_world` package

Using cargo, we create a new project using the cargo new --bin xxx where xxx is the name of our project. Our project’s file tree once created will look like:

└── hello_world
    ├── Cargo.toml
    └── src

2 directories, 2 files

As you can see, is our main sourec file. Additionally you’ll see a cargo configuration file, Cargo.toml, that will specify how cargo will interpret the build process. The default Cargo.toml file looks like:

name = "hello_world"
version = "0.1.0"
authors = ["Rob Baruch <>"]
edition = "2018"

# See more keys and their definitions at


Pretty self-explanatory. Take a look at the dependencies. Here we have none. But if we wanted to use any external packages, we can add them here using vim, and adding the crate to the Cargo.toml file. For example, if we want to add the rand located at, add this to the end of the [dependencies] block:

rand = "0.8.3"

Now we can use cargo update to add the rand crate to our project.

$ cargo update
    Updating index
      Adding cfg-if v1.0.0
      Adding getrandom v0.2.2
      Adding libc v0.2.94
      Adding ppv-lite86 v0.2.10
      Adding rand v0.8.3
      Adding rand_chacha v0.3.0
      Adding rand_core v0.6.2
      Adding rand_hc v0.3.0
      Adding wasi v0.10.2+wasi-snapshot-preview1

We can build our project using cargo build

$ cargo build
  Downloaded getrandom v0.2.2
  Downloaded rand_chacha v0.3.0
  Downloaded rand_core v0.6.2
  Downloaded cfg-if v1.0.0
  Downloaded ppv-lite86 v0.2.10
  Downloaded libc v0.2.94
  Downloaded 6 crates (602.2 KB) in 0.28s
   Compiling libc v0.2.94
   Compiling getrandom v0.2.2
   Compiling cfg-if v1.0.0
   Compiling ppv-lite86 v0.2.10
   Compiling rand_core v0.6.2
   Compiling rand_chacha v0.3.0
   Compiling rand v0.8.3
   Compiling hello_world v0.1.0 (/Users/robert/src/rust/tmp/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 7.88s

And run it as well with cargo run

$  cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello_world`
Hello, world!

Wait, we didn’t write any code!!! How did we get hello world??? Turns our the default soure when cargo creates a project looks like:

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

There you have it. the basic rust hello world program! From here, we’ll dive into the language and learn more …

comments powered by Disqus