Skip to content

Rust programming

Programming


Install and rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • rustup: toolchain manager
  • rustc: compiler
  • cargo: build & package manager
  • crate: smallest compilation unit in Rust (lib/bin)
  • crates.io: rust's official package registry
load rust into shell
source ~/.cargo/env

rust toolchain

A toolchain is a complete Rust environment consisting: - rustc - cargo - Rust standard library - optional components (clippy, rustfmt)

rustup

rustup manages Rust versions, targets, and components on your system.

autocomplete

Add rustup autocomplete

1
2
3
source <(rustup completions bash)
# add to bashrc
echo 'source <(rustup completions bash)' >> ~/.bashrc

update

update and upgrade toolchain and components

rustup update
# its like apt update + apt upgrade

Components

A component is an optional tool that plugs into a Rust toolchain.

  • rustfmt: Formatting
  • clippy: Linting
1
2
3
4
# add to active toolchain
rustup component add rustfmt
# for specific toolchain
rustup component add rustfmt --toolchain nightly

Hello world

Simple hello world build and run - Add aarch target and build it for ARM

cargo new hello_world
1
2
3
4
5
cargo run        # build + run
cargo build      # build only
cargo check      # fast compile check
cargo fmt        # format code
cargo clippy     # lint code

Add target

install rust target
rustup target add aarch64-unknown-linux-gnu
install cross compiler
sudo apt update
sudo apt install gcc-arm-linux-gnueabihf
./.cargo/config.toml
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
build with aarch target
cargo build --release --target aarch64-unknown-linux-gnu

Reference