Skip to content

Latest commit

 

History

History
195 lines (160 loc) · 7.77 KB

rust.md

File metadata and controls

195 lines (160 loc) · 7.77 KB

Rust

Books

Articles and blogs

Getting started

Install system packages

# Make sure rust isn't installed, because we'll use `rustup` to manage rust and cargo installations
# Linux: apt remove cargo rust
# macOS: brew uninstall rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Ensure that ~/.cargo/env is sourced in your environment. rustup may update ~/.bashrc, ~/.profile, and/or ~/.zshenv accordingly, but you may wish to modify these files according to your needs.

if [[ -f "${HOME}/.cargo/env" ]]; then
    source "${HOME}/.cargo/env"
fi

Update Rust on the stable, beta, or nightly channel

#channel=beta
#channel=nightly
channel=stable
rustup update ${channel}
rustup default ${channel}

Create a project

# Use the binary template by default: --bin
cargo new ${project_name}

# Or use the library template: --lib
# cargo new --lib ${library_project_name}

cd ${project_name}
vim Cargo.toml

# Use the toolchain from the nightly channel for this project/directory
rustup override set nightly

cargo build
cargo run

Macros

Features and crate-level configuration:

// Requires Rust from the nightly channel as of rustc 1.59.0-nightly (e012a191d 2022-01-06)
#![feature(mixed_integer_ops)]

// Allow modules to have the same name as their parent module
#![allow(clippy::module_inception)]

// Show more lint warnings
#![warn(clippy::all, clippy::pedantic)]

Cargo

# List features enabled for each dependency
cargo tree -f "{p} {f}"
# More verbose version of the above
cargo tree -e features

Visual Studio Code

./.vscode/tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "cargo build",
      "type": "shell",
      "command": "cargo build",
      "args": [],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": []
    },
    {
      "label": "cargo run",
      "type": "shell",
      "command": "cargo",
      "args": [
        "run"
      ],
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "problemMatcher": []
    }
  ]
}

Crates - Frameworks and libraries

Name Description
anyhow Concrete Error type built on std::error::Error
clap Parse command line arguments
chrono Date and time library
chrono-tz Timezone library
crossterm Library for controlling terminals
cursive Text User Interface (TUI) library. (Comparison to tui).
enum-iterator #[derive(IntoEnumIterato) for enums
gtk-rs GTK4, Cairo, glib, etc bindings
OnceCell Single assignment cells and lazy values
rand Generate random numbers
Serde JSON Serialize and deserialize JSON
strum Various #[derive(...)] for enums
tempfile Create temporary files or directories
tera Template engine inspired by Jinja2
tokio Async runtime, including IO, networking, scheduling, and timers
tui Text User Interface (TUI) library. (Comparison to cursive).
uuid Generate and parse UUIDs

Software

  • bevy - Game engine

Debugging

Troubleshooting

$ cargo build
   Compiling space_time_rewind v0.1.0 (/home/andornaut/src/github.com/andornaut/space-time-rewind)
thread 'rustc' panicked at 'failed to lookup `SourceFile` in new context', compiler/rustc_query_impl/src/on_disk_cache.rs:514:22

Fix: cargo clean && cargo build