You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Check if rustup is already installed
rustup --version
If not, download and run rustup-init.exe from rust-lang.org
Installation Profiles
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
Post-Installation Checks
# 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
rustup docs
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)
Additional rustup Commands
Command
Description
rustc --version
Check installed Rust Compiler version
rustup docs or rustup doc
Open local documentations index
rustup update
Update all installed rustup components
rustup self uninstall
Uninstall Rust and all its components
Hello World
Create a hello-world project folder with the following structure
hello-world/
├── target/
└── src/
└── main.rs
Open main.rs in an editor and add the following codes to main.rs
/****************************************//* Print "Hello world!" to the console. *//****************************************//// The entry-point of the program.fnmain(){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.exe
main() 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 rustfmt is used with cargo
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)
Compiling Rust Code
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 .pdb file 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: rustc alone is not enough for larger projects
We use cargo instead
cargo allows to better manage a larger project
Hello Cargo
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
Cargo comes with Rust when installing via rustup
Full documentation for cargo is available in the Cargo Book Online or Offline
rustup docs --cargo
Main Commands
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 --release when building for final production