Skip to main content

Getting Started with Rust

Setup the environment​

To get started with Rust, you need to download and install it first. You can find installation instructions on the official Rust website: https://www.rust-lang.org/tools/install

Once you have Rust installed, you can create a new Rust project using Cargo, the official Rust package manager. Here's how you can create a new project:

$ cargo new app_name
$ cd app_name

This command creates a new Rust app and sets up the project directory structure.

Project structure​

When starting a new Rust project, it's important to have a clear and organized project structure in order to make the project more maintainable and scalable. Here is an example of a project structure for a new Rust project:

.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs

Let's break down what each file and directory is for:

Files: Cargo.lock and Cargo.toml​

These files are generated by the Cargo package manager, which is the official package manager for Rust. Cargo.toml is where you define your project's dependencies and configuration options, while Cargo.lock is an automatically generated file that tracks the exact versions of your dependencies.

Directory: src/​

This directory is where you put your project's source code. You can find the main.rs file in this directory, which is where you define the executable code for your project.

Directory: target/​

This directory is where Cargo stores compiled binaries and libraries. The debug directory contains the debug build of your project, while the release directory contains the optimized release build of your project.

Overall, this project structure provides a clear separation between your project's source code and the build artifacts, making it easy to maintain and scale your project as it grows.

Running the project​

To run the project, you can use the cargo run command:

$ cargo run

If you want to build the project without running it, you can use the cargo build command:

$ cargo build

If you want to check for errors before running the app, you can use the cargo check command:

$ cargo check

Finally, if you want to make a production-ready executable, you can use the cargo build --release command:

$ cargo build --release

✅ In Summary​

This guide should be enough to get you started with Rust and teach you the basic commands for building and running a Rust project.

✅ Resources​