Rust
Information
Rust is a modern systems programming language that aims to provide memory safety, concurrency, and high performance without sacrificing control over system resources. Developed by Mozilla and now maintained by the Rust Foundation, it is designed to address common issues found in other languages like C and C++, such as null pointer dereferencing and buffer overflows, through its powerful ownership model and strict compile-time checks. Rust’s expressive type system, robust package manager (Cargo), and thriving ecosystem make it a popular choice for building reliable and efficient software, from operating systems and game engines to web servers and command-line tools.
Install
Installing Rust on Linux, MacOS, or WSL involves using the rustup
tool, which manages the Rust installation and its associated tools.
You can install rustup
by running a simple curl
command in the terminal and following the prompts to complete the setup.
After configuring your shell and verifying the installation, you’ll have a fully functional Rust development environment ready for use.
-
Install Rust using
rustup
:Open a terminal and run the following command to download and install the
rustup
tool, which will manage your Rust installation.curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
Follow the prompts during the installation process to proceed with the default installation options. If the installation is successful, you will see a message indicating that Rust has been installed.
-
Configure Your Shell:
After the installation is complete, you need to configure your current shell session to use Rust by running the following command:
source $HOME/.cargo/env
This command ensures that Rust and its associated tools are available in your shell environment.
-
Verify the Installation:
To confirm that Rust has been installed correctly, check the version of the Rust compiler with the following command:
rustc --version
This command should display the installed version of Rust, indicating that the installation was successful.
-
Install Required Dependencies:
Rust requires a linker to combine its compiled outputs into one file. You may need to install additional tools if you encounter linker errors.
On Ubuntu/Debian:
sudo apt update sudo apt install build-essential
On Fedora:
sudo dnf groupinstall 'Development Tools'
On Arch Linux:
sudo pacman -S base-devel
On MacOS:
Install Xcode command line tools, which include a C compiler and linker.
xcode-select --install
-
Update Rust:
Keep your Rust installation up-to-date with the following command:
rustup update
-
Configure Your Development Environment:
To make the most of Rust, you should set up a development environment.
Install Visual Studio Code (optional):
VS Code is a popular code editor with excellent support for Rust.
On Linux:
sudo snap install --classic code
On MacOS:
Download and install from the official website.
Install the Rust extension for VS Code:
Open VS Code and install the “rust-analyzer” extension for Rust language support.
Set up your first Rust project:
cargo new hello_world cd hello_world cargo run
This will create a new Rust project named
hello_world
, navigate into the project directory, and run the sample application. -
Explore Rust Documentation:
Rust has extensive documentation and a friendly community. Here are some resources to get you started:
Update
Updating Rust is simple and straightforward using the rustup
tool.
By running the rustup update
command in your terminal, you can ensure that your Rust installation and all associated components are kept up-to-date with the latest stable versions.
-
Open a Terminal:
Ensure that you have access to a terminal on your Linux, MacOS, or WSL system.
-
Run the
rustup
Update Command:Use the following command to update Rust and all installed components to the latest version:
rustup update
This command will download and install the latest stable version of Rust and update all installed toolchains.
-
Verify the Update:
After the update process is complete, verify that Rust has been updated successfully by checking the version:
rustc --version
This command should display the latest version of Rust, confirming that the update was successful.
-
Clean Up Old Versions (optional):
To free up disk space, you can remove older versions of Rust that are no longer needed by running:
rustup self uninstall
This command will prompt you to confirm the removal of old toolchains and components.
Cargo
Cargo is the Rust package manager and build system, designed to streamline the process of managing dependencies, compiling code, and building projects. It simplifies tasks like creating new projects, running tests, and generating documentation with easy-to-use commands. Cargo ensures that Rust developers can efficiently manage their projects’ libraries and tools, maintaining consistency and reliability throughout the development lifecycle.
-
Install Rust and Cargo:
Cargo comes bundled with Rust, so installing Rust also installs Cargo. If you haven’t installed Rust yet, follow the installation steps below:
Open a terminal and run the following command:
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
Follow the prompts to complete the installation. Once Rust is installed, Cargo will be installed automatically.
-
Verify Cargo Installation:
To ensure Cargo is installed correctly, check its version by running:
cargo --version
This command should display the installed version of Cargo, confirming the installation was successful.
-
Create a New Rust Project:
Use Cargo to create a new Rust project by running:
cargo new my_project cd my_project
This command creates a new directory named
my_project
with the necessary files and structure for a Rust project. -
Build the Project:
To compile your Rust project, navigate to your project directory and run:
cargo build
This command compiles your project and produces an executable in the
target/debug
directory. -
Run the Project:
After building your project, you can run it with:
cargo run
This command compiles (if necessary) and runs your project, displaying any output in the terminal.
-
Add Dependencies:
To add external libraries (dependencies) to your project, edit the
Cargo.toml
file in your project directory and add the desired dependencies under the[dependencies]
section. For example:[dependencies] serde = "1.0"
After adding dependencies, run:
cargo build
Cargo will download and compile the specified dependencies for your project.
-
Run Tests:
To run tests defined in your project, use:
cargo test
This command compiles and runs any tests found in the
tests
directory or within your source files. -
Generate Documentation:
Cargo can generate documentation for your project and its dependencies. To generate and view the documentation, run:
cargo doc --open
This command generates HTML documentation and opens it in your default web browser.
-
(Optional) Install Cargo Watch:
Cargo Watch automatically runs Cargo commands when file changes are detected, useful for continuous development.
Install Cargo Watch:
cargo install cargo-watch
Use Cargo Watch:
To automatically recompile your project when you make changes, run:
cargo watch -x build
For automatic testing on file changes, use:
cargo watch -x test
Guides
-
The Rust Programming Language Book
(From the Official Rust Lang Website) This book assumes that you’ve written code in another programming language but doesn’t make any assumptions about which one. We’ve tried to make the material broadly accessible to those from a wide variety of programming backgrounds. We don’t spend a lot of time talking about what programming is or how to think about it. If you’re entirely new to programming, you would be better served by reading a book that specifically provides an introduction to programming.
-
The aim of this tutorial is to take you to a place where you can read and write enough Rust to fully appreciate the excellent learning resources available online, in particular The Book. It’s an opportunity to try before you buy, and get enough feeling for the power of the language to want to go deeper.
-
This small book describes how to use Rust and WebAssembly together.
-
This Rust Cookbook is a collection of simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem.
Examples
- These are some examples of repos, demos, and source code written in rust!
MicroService
This is a quick repo / guide on a Rust MySQL Microservice! Official Repo
Youki
Youki is an Open Container Initiative runtime specification library written in Rust.
Official Youki Repo
There are some issues with some devices within CGroups v2
that should be noted.
Since youki is a low-level runtime, its recommend that you combine it with a high-level runtime, such as Docker / Podman.
Axum
- Axum is an ergonomic and modular Rust web application framework.
- Github Repo
- Axum can be extended through Tower, which is an ecosystem of middleware, services and utilities
eGUI
This crate is a user-friendly, easy-to-integrate, immediate-mode GUI library designed for rapid development of interactive applications in Rust.
egui Dependency
-
There are some dependencies that you might require when working with
egui
:sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
- Note: These dependencies are for Ubuntu / WSL.
-
Making sure the
rustup
has thewasm
components.rustup target add wasm32-unknown-unknow
-
Installing
Trunk
cargo install --locked trunk