logo

Rust

CLI

  • cargo: the package manager (downloads dependencies of your program).
  • rustc: the compiler (turns rust code into an executable).
  • rustup: the updater (rustc and cargo).
# Create a new project
$ cargo init

# Create a new project `foo`
$ cargo new foo
$ cargo new --edition 2018 foo

# Check if project would compile
$ cargo check

# Build
$ cargo build               # in debug mode
$ cargo build --release     # in release mode

# Run
$ cargo run

# Test
$ cargo test

# Show dependency graph
$ cargo tree

# Get toolchain info
$ rustup show
$ rustup toolchain list
$ rustup component list

# Check and upgrade
$ rustup check
$ rustup update

# Install target for cross compilation, e.g. aarch64-linux-android
$ rustup target install aarch64-linux-android

Path

  • crate:: indicates the root of the current crate, e.g. crate::foo::bar
  • :: indicates the root of the external crate, e.g. external crate foo: ::foo::bar

External Crates

  • If you're using Cargo: add to Cargo.toml
  • If you're not using Cargo: pass --extern flags to give rustc the location of external crates

Config

  • ~/.cargo/config.toml: Cargo config
  • ~/.rustup: toolchain config and downloads

keywords

  • Strict keywords: can only be used in their correct contexts; cannot be used as names.
  • Reserved keywords: not used but reserved for future use; cannot be used as names.
  • weak keywords: These keywords have special meaning only in certain contexts. Only 3: macro_rules, union,static

FAQ

Why Rust?

  • syntactically similar to C++, but can guarantee memory safety.
  • achieves memory safety without garbage collection.

Crucial features:

  • move semantics
  • minimal runtime
  • trait-based generics
  • zero cost abstractions
  • pattern matching
  • type inference.

Versions and Editions

A new version is release every a month or two; a new edition is every 3 years: 2015, 2018, 2021

"Editions" are Rust's way of introducing changes into the language that would not otherwise be backwards compatible. The choice of edition is made per crate as part of its Cargo.toml

Edition is similar to feature gates, e.g. it decides if async is treated as a keyword (in latest editions) or not (in early editions when async/await was not introduced).

https://doc.rust-lang.org/edition-guide

What are Modules in Rust?

  • Importing Modules with use
  • Re-exporting with pub use

What are "Cargo" and "Crate" in Rust?

  • Cargo: a build system and package manager.
  • Crate: a library or package.

Each crate has an implicit root module that contains the code for that crate. You can then define a tree of sub-modules under that root module. Modules allow you to partition your code within the crate itself.

Crate Registry: https://crates.io/

Methods vs Associated Functions

You can have two kinds of procedures: methods and associated functions.

  • methods: exists inside the struct implementation and receives the &self parameter, indicating that it needs to be called by an instance with a ..
  • associated functions: called without an instance, it can be compared with the Java static functions. They are normally are used as a constructor of instances.

When to use &self, self and &mut self?

  • &self: when read-only reference is rquired.
  • self: when a value is to be consumed by the function.
  • &mut: when a value needs to be mutated by the function.

Notable Rust Projects

Trending Rust projects on GitHub: https://github.com/trending/rust

Kata Containers

A standard implementation of lightweight Virtual Machines (VMs) that feel and perform like containers, but provide the workload isolation and security advantages of VMs.

https://github.com/kata-containers/kata-containers

Caliptra

Root of Trust; for datacenter-class SoCs like CPUs, GPUs, DPUs, TPUs.

Caliptra software is primarily using Rust: https://github.com/chipsalliance/caliptra-sw

Dracut

Dracut is a set of tools that provide enhanced functionality for automating the Linux boot process. (Currently 5% Rust) Ubuntu replaces initramfs-tools with dracut.

https://github.com/dracut-ng/dracut-ng

Rustls

A modern TLS library in Rust. Competing with openssl and boringssl.

https://github.com/rustls/rustls