Getting started with Rust

Abindran RAbindran R
3 min read

Rust is an ahead-of-time compiled language, which means Rust code needs to be compiled first. The resulting binary can run without needing Rust installed, unlike JavaScript and Python, which require Node.js and Python to be installed on the system.

The file extension for Rust is .rs.

Hello World using rustc

Create a file named hello_world.rs and write the following code.

fn main(){
  println!("hello world");
}

Run the following command in the terminal:

rustc hello_world.rs

This will create a compiled file. Then, run the compiled file with:

./hello_world

In the code above, println! is a Rust macro, not a Rust function.

In this example, you have seen how to run a Rust program using rustc.

Rust with cargo

Moving forward, we will use Rust's package manager, Cargo, to create and run Rust projects.

Cargo is included by default with the Rust installation.

cargo --version // To check the Cargo version

This command can be used to verify the Cargo installation.

To create a new project with Cargo, use:

cargo new hello_world

This will generate a Cargo.toml file and a src/main.rs file.

Cargo.toml is similar to package.json in JavaScript projects.

While creating the cargo projects a new git repository will be initialized.

The generated cargo.toml file contains:

[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

[dependencies]

TOML stands for Tom’s Obvious, Minimal Language.

  • The first line, [package], acts as a section heading, signaling that the following statements are related to configuring a package.

  • The next three lines specify the configuration details required by Cargo to compile your program: the package name, version, and the Rust edition to be used.

  • The last line, [dependencies], begins a section where you can define any dependencies for your project. In Rust, these packages of code are referred to as crates.

Cargo CLI Commands

You can create a project with cargo new.

To build a project, use cargo build.

To build and run a project in one step, use cargo run.

To check for errors without creating a binary, use cargo check.

By default, Cargo stores build results in the target/debug directory instead of the project directory.

When you're ready for release, use cargo build --release to compile with optimizations, which will place the executable in target/release.

Converting an existing Rust project into a Cargo project

You can convert a rustc project into a Cargo project.

If you started with a project that doesn't use Cargo, like the "Hello, world!" example, you can turn it into a Cargo project. Simply move the project code into the src directory and create a Cargo.toml file in the root of the project.

What's Next?

In the next post, I'll cover variables, data types, and functions in Rust.

Until then, signing off! See you in the next blog post of the Rust series. 🚀

0
Subscribe to my newsletter

Read articles from Abindran R directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abindran R
Abindran R