- Using
rustup- Command-line tool for managing Rust versions
- Installs Rust, Cargo, and additional developer tools
| Settings | Value | Default Value |
|---|---|---|
| Home Directory | ~/.rustup |
|
| Environment Variable | RUSTUP_HOME |
~/.rustup |
RUSTUP_HOMEcan be used to customize installation
| Settings | Value | Default Value |
|---|---|---|
| Home Directory | ~/.cargo |
|
| Environment Variable | CARGO_HOME |
~/.cargo |
CARGO_HOMEcan be used to customize installation- Paths are added to:
~/.profile~/.bash_profile~/.bashrc- Environment Variables
- Need a C-Compiler & Linker
- Used to join compiled outputs into one file
- For Linux, install
gccorclang build-essential=>dpkg-dev,g++,gcc,libc6-dev,make
# GCC
# ---
# Check if already installed
gcc --version
# If not, install via build-essential
sudo apt-get install build-essential# Clang
# -----
# Check if already installed
clang --version
# If not, search available versions
apt-cache search clang
# Install latest version available: In this case, v15
sudo apt-get install clang-15 --install-suggests- For MacOS, install XCode's compiler
xcode-select --install- Check if
rustupis already installed
# Check if rustup is already installed
rustup --version- If not, install
rustupviacurl - This also bootstraps the installation of the Rust dev tools via
rustup
# Install rustup via curl, then Rust dev tools
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh- For Windows, need to install MSVC Build Tools For Visual Studio 2013 or later
- It can be installed with Visual Studio Installer
- Install Visual Studio
- Choose Desktop Development with C++
- Choose appropriate Windows SDK
- To check the version of the compiler, call with no argument:
- x64:
C:\"Program Files"\"Microsoft Visual Studio"\2022\Community\VC\Tools\MSVC\<<version>>\bin\Hostx64\x64\cl.exe - x86:
C:\"Program Files"\"Microsoft Visual Studio"\2022\Community\VC\Tools\MSVC\<<version>>\bin\Hostx64\x86\cl.exe
- x64:
- Check if
rustupis installed
# Check if rustup is already installed
rustup --version- If not, download and run
rustup-init.exefrom rust-lang.org
| Installation Profile | Included |
|---|---|
| Minimal | rustc,rust-std,cargo |
| Default (Recommended) | Minimal + rust-docs, rustfmt, clippy |
| Complete | Default + All components available through rustup:Every component ever included in the metadata This option should never be used except for maintainers |
# Check the installed toolchain
rustup toolchain list
# List the installed components
rustup component list --installed| Component | Description |
|---|---|
cargo |
Package Manager / Build Tool |
clippy |
A collection of lints to catch common mistakes |
rust-docs |
All Rust Documentations installed locally for offline use: Access via rustup docs or rustup doc command |
rust-std |
Rust Standard Library |
rustc |
Rust Compiler |
rustfmt |
Rust Formatter to follow standard style guidelines |
- Allows to access Rust Documentations offline
- There are multiple options available
| Command | Docs For |
|---|---|
rustup docs |
Rust Documentation Index |
--alloc |
The Rust Core Allocation and Collections library |
--book |
The Rust Programming Book (The Book) |
--cargo |
The Cargo Book |
--core |
The Rust Core Library |
--edition-guide |
The Rust Edition Guide |
--embedded-book |
The Embedded Rust Book |
--help |
Prints help about rustup docs commands |
--nomicon |
The Rustonomicon: The Dark Arts of Advanced and Unsafe Rust Programming |
--path |
Print the path to the local offline documentations |
--proc_macro |
A support library for macro authors when defining new macros |
--reference |
The Rust Reference |
--rust-by-example |
A collection of runnable examples that illustrate various Rust concepts and standard libraries |
--rustc |
The Rust Compiler Book |
--rustdoc |
The rustdoc Book: Documentation Generation tool for Rust projects |
--std |
Standard library API documentation |
--test |
Library to support code for rustc's built-in unit-test and micro-benchmarking framework |
--unstable-book |
The Rust Unstable Book |
--toolchain <toolchain> |
Rust documentations specific to a Toolchain (The toolchain must be installed) |
| Command | Description |
|---|---|
rustc --version |
Check installed Rust Compiler version |
rustup docrustup docs |
Open local documentations index |
rustup update |
Update all installed rustup components |
rustup self uninstall |
Uninstall Rust and all its components |
- Create a
hello-worldproject folder with the following structure
hello-world/
├── target/
└── src/
└── main.rs
- Open
main.rsin an editor and add the following codes tomain.rs
/****************************************/
/* Print "Hello world!" to the console. */
/****************************************/
/// The entry-point of the program.
fn main() {
println!();
println!("Hello world!");
println!();
}
// ON LINUX:
// Compile: rustc src/main.rs -o target/main
// Execute: ./target/main
// ON WINDOWS:
// Compile: rustc src\main.rs -o target\main.exe
// Execute: .\target\main.exemain()function is the entry-point in every executable Rust program- No parameters
- No return values
- Rust requires curly brackets around all function bodies
- Recommended style to place the opening curly bracket on the same line as the function declaration
- Automatic formatter tool
rustfmtis used withcargo - Rust style is to indent with 4 spaces, not tabs
macroname!()is a macro- Functions and Macros are different in Rust
- Macros do not always follow the same rules as Functions
- E.g.
println!()is a macro
- Most lines in Rust end with a semicolon (
;), but not all of them (see expression vs statement)
- Rust is Ahead-Of-Time (AOT) compiled language
- Produces a single executable file
- Similar to C, C++, Go
- Compiler:
rustc
- On Windows, compiling also outputs a
.pdbfile for debugging info
# Compiling on Linux
rustc src/main.rs -o target/main
# Executing on Linux
./target/main# Compiling on Windows
rustc src\main.rs -o target\main.exe
# Executing on Windows
.\target\main.exe- NOTE:
rustcalone is not enough for larger projects- We use
cargoinstead cargoallows to manage a larger project better
- We use
- Cargo is the Build System and Package Manager for Rust
- Allows to manage projects
- Build codes
- Download dependencies
- Build libraries
- It is better to start a project using
cargo- The vast majority of Rust projects are built with
cargo
- The vast majority of Rust projects are built with
- Cargo comes with Rust when installing via
rustup - Full documentation for
cargois available in the Cargo Book Online or Offline
rustup docs --cargo- The commands are the same no matter which operating system
| Command | Description |
|---|---|
cargo new |
Create a new project |
cargo build |
Compile the source codes in the current project (debug mode) |
cargo build --release |
Compile the source codes with optimizations (release mode) |
cargo run |
Build + Run the project (debug mode) |
cargo run --release |
Build + Run the project (release mode) |
cargo check |
Check compile status without compiling (debug mode) |
- NOTE: Always use
--releasewhen building for final production- Can greatly improve the size of binary
- Also adds additional optimizations
cargo --versioncargo new hello-cargo- Creates a new project directory:
hello-cargo
hello-cargo/
├── .git/
├── .gitignore
├── Cargo.toml
└── src/
└── main.rs
| Folder or File | Description |
|---|---|
.git |
Git is the default Version Control System (VCS) Not generated if already within a Git project Can override with --vcs flag |
.gitignore |
Git is the default Version Control System (VCS) Not generated if already within a Git project Can override with --vcs flag |
Cargo.toml |
Manage project configs and dependencies/crates |
src/main.rs |
The entrance of the program |
- NOTE: Git-related files are not generated if already within an existing Git repository
Cargo.tomlcan have multiple sections but the following are defaulted
| Section | Description |
|---|---|
[package] |
- Configure the project as a Cargo package - Also adds compiler info - name - Name of the Package- version - Version of the Package- edition - The Rust edition to use- See more keys and their definitions from the Cargo Book - See more details about Rust Editions from The Rust Edition Guide |
[dependencies] |
- List of crates dependencies for the project - If none is listed, then the project will install no additional dependency crates |
[package]
name = "hello-cargo"
version = "0.1.0"
edition = "2024"
[dependencies]- All source codes should live in
src- Except for non-source code files which can be in top-level project directory
- README files
- License information
- Configuration files:
Cargo.toml,Cargo.lock - Anything else not related to code
- Except for non-source code files which can be in top-level project directory
- Cargo helps organize projects
- There is a place for everything
- Everything is in its place
- It is possible to convert a manually-managed project to Cargo project
- Move the project code into the
srcdirectory - Create an appropriate
Cargo.tomlfile: Can be done by runningcargo init
- Move the project code into the
/*****************************************/
/* Print "Hello, world!" to the console. */
/*****************************************/
/// The entry-point of the program.
fn main() {
println!();
println!("Hello world!");
println!();
}
// Check: $ cargo check
// Build: $ cargo build
// Build + Run: $ cargo run
// Execute: $ ./target/debug/hello-cargo
// Build Release: $ cargo build --release
// Build + Run Release: $ cargo run --release
// Execute Release: $ ./target/release/hello-cargo- Simply
cdinto the project and runcargo build
cd hello-cargo
cargo build- It builds from
srcfolder- Default build is Debug Build
dev [unoptimized + debuginfo] - Creates an executable file in
target/debug
- Default build is Debug Build
- First-time build also creates
Cargo.lock- Keeps track of the exact versions of dependencies/crates in the project
- Managed by Cargo: This file is automatically generated by Cargo
- Never need to change this file manually: It is not intended for manual editing
- We can use
cargo runto build and run at once - Convenience for
cargo build+ run binaries
cd hello-cargo
cargo run- NOTE: Cargo will only rebuild if there are diff changes detected
- NOTE: We can also use
cargo run --releasefor the release option
cargo checkallows to check if a project can build without generating the binaries- Much faster than
cargo build- Skips the step of producing an executable
- Still generates
targetfolder but not the executable file - Helpful for multiple periodical compiling-checks when writing code (e.g. watching for changes, debugging errors)
cd hello-cargo
cargo check- Default build is Debug Build (
dev [unoptimized + debuginfo]) - To build for release, we use the
--releaseoption
cd hello-cargo
cargo build --release- Compiles with optimizations: Binary size is much smaller
- Creates an executable file in
target/release - Makes Rust code run faster
- But longer build-time is needed
- Only use this for building the final program
- Cargo is invaluable for large and complex projects
- Additional tooling for project management
- We can use
cargo buildon any project made withcargo