Rust
Updated: 2025-08-20Installation
Install using rustup.
Usage
rustup
rustup packages all things rust, including rustc,cargo,rust-analyzer,
and the std lib.
rustc
rustc compiles a binary. Usually we use cargo instead of rustc directly.
cargo
cargo is the package manager and utility to build/run/check a rust project.
See cargo
rust-analyzer
Syntax
fn main() is always the first function to run in a rust program (probably true
for most programming languages).
Most lines of Rust code end with a semicolon.
Rust importst the std
prelude. Other std libraries
also have preludes, but need to be explicitly brought in to scope with use.
println!() is a macros that prints a string to the screen.
Variables are immutable by default. To make a mutable variable, you need to
prefix with mut → let mut apples = 5;.
:: → associated function of a type. For example, String::new() is the
new() function of String.
An associated function is a function that’s implemented on a type[...]. You’ll find a
newfunction on many types because it’s a common name for a function that makes a new value of some kind.
diff between types, functions and methods ? (and traits ...)
A handle is just an indirect reference to a resource.
Instead of directly giving you the resource (like the actual keyboard device, or a file), the system gives you a small object or identifier that you can use to interact with it.
You can think of it as a ticket or remote control that lets you operate something else.
use std::io could also be directly called with std::io::Sdin or somthing.
The
&indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times.
It’s often wise to introduce a newline and other whitespace to help break up long lines when you call a method with the
.method_name()syntax.
Enum & methods
Result return value is an enum type, which means it can have multiple
possible states, called variants.
Result's variants are Ok and Err.
Values of the Result type, like values of any type, have methods defined on them.
Macros
println() is a function, println!() is a macros. Rust macros are a way to
write code that generates code to extend Rust syntax. Macros don’t always follow
the same rules as functions.
Dependencies
Where to look for crates a.k.a. libraries
Update a dependency to the latest version
# 1. Edit Cargo.toml
[dependencies]
crate_name = "x.y" # set to desired/latest version
# 2. Update
cargo update -p crate_name
# 3. Verify
cargo tree | grep crate_name
cargo update -p crate_name→ updates only that dependencycargo update→ updates all dependencies- Loosen version in
Cargo.tomlif updates are blocked by constraints
Compiling
Types of builds
--dev builds are optimized at level 0, which means it's unoptimized, but is
also faster to compile.
cargo build --dev
--release flag indicates an optimization level of 3, the maximum. It takes
longer to compile.
cargo build --release
Cross Compiling from Archlinux
Requirements
You first need rustup to be able to add targets.
Windows
sudo pacman -S mingw-w64-gcc
Linux
Since we already on linux, we don't need much. However, to compile to ARM, we need:
sudo pacman -S aarch64-linux-gnu-gcc
and then set it as the linker for the ARM target in .cargo/config.toml(local):
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
Cargo ignores linker settings in Cargo.toml. It only reads them from
.cargo/config.toml (project-local) or ~/.cargo/config.toml (global).
MacOS
I don't know yet...
Maybe this MacOS Cross-Compiler - github.com
https://studios.ptilouk.net/superfluous-returnz/blog/2022-03-16_macos.html https://doc.rust-lang.org/beta/rustc/platform-support/apple-darwin.html https://doc.rust-lang.org/beta/rustc/platform-support.html https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html/ ← seems promissing
https://github.com/cross-rs/cross
https://github.com/tpoechtrager/osxcross