An introduction into Rust. Part 2: Building and Running, Creating a Package, Numerical Operations

applied.math.coding
3 min readJan 7, 2022

Welcome back to the follow-up of the introduction into Rust! This post is all about to get your first Rust program built and executed.

You can follow the code examples on your local machine by ensuring Rust has been successfully installed as described in the previous post.

Running the first program:

Running the first code in Rust can be as easy as just going to this website: https://play.rust-lang.org/. This is Rust’s playground that lets you quickly check out short code snippets.

In order to compile and run Rust code locally, you already have installed everything necessary if you followed the section ‘Installation’.

  1. create some folder: mkdir my-first-rust
  2. cd my-first-rust
  3. add a file named main.rs
  4. add the following content to main.rs:
fn main() {
println!("Hello, world!");
}

5. run the compiler: rustc main.rs

6. execute the compiled resource: ./main or ./main.exe

This should print Hello World to the terminal.

Packaging with cargo:

Although the above approach is absolutely valid, it is recommended to use the package manager cargo to create and run your Rust project. This comes with many advantages like dependency management, automatic tests, …

To create a new Rust project you would just run in a terminal:

cargo new my-rust-project

This creates a folder my-rust-project where you find a file named Cargo.toml. This is similar to the well known package.json if you come from JavaScript. It’s contents looks like:

[package]
name = “my-rust-project”
version = “0.1.0”
edition = “2021”
[dependencies]

--

--

applied.math.coding

I am a Software Developer - Java, Rust, SQL, TypeScript - with strong interest doing research in pure and applied Mathematics.