# Rust

## 🧾 Overview

Rust provides performance, memory safety, and predictable resource usage for APIs, workers, and bots. Install with `rustup` to manage toolchains locally before deploying.

***

## 📥 Installation (choose your OS)

{% tabs %}
{% tab title="🪟 Windows" %}
{% tabs %}
{% tab title="rustup-init (Official)" %}
{% stepper %}
{% step %}
Download the installer: <https://win.rustup.rs/> (or run command below in PowerShell)
{% endstep %}

{% step %}
Run and accept defaults (installs to user profile).
{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="Alternative (PowerShell):" %}

```bash
irm https://win.rustup.rs -UseBasicParsing | iex
```

{% endtab %}
{% endtabs %}

#### Verify

```bash
rustc --version
cargo --version
```

#### Update Toolchain

```bash
rustup update
```

{% endtab %}

{% tab title="🐧 Linux" %}
Install via rustup script:

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
rustc --version
cargo --version
```

#### Add Build Essentials (Debian/Ubuntu)

```bash
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev
```

#### Update

```bash
rustup update
```

#### Remove Toolchain

```bash
rustup self uninstall
```

{% endtab %}

{% tab title="🍎 macOS" %}
Install with rustup:

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
rustc --version
cargo --version
```

Add build tools (if not present):

```bash
xcode-select --install  # prompts for developer tools
```

Update:

```bash
rustup update
```

{% endtab %}
{% endtabs %}

***

## 🧰 Toolchains & Components

{% stepper %}
{% step %}
List installed

```bash
rustup toolchain list
```

{% endstep %}

{% step %}
Add nightly (optional)

```bash
rustup toolchain install nightly
```

{% endstep %}

{% step %}
Set default

```bash
rustup default stable
```

{% endstep %}

{% step %}
Add components (example)

```bash
rustup component add clippy rustfmt
```

{% endstep %}
{% endstepper %}

***

## 🗂 Project Init

Create new project:

```bash
cargo new myapp
cd myapp
cargo build
```

Run:

```bash
cargo run
```

Format & lint:

```bash
cargo fmt -- --check
cargo clippy -- -D warnings
```

***

## 🗃 Common Cargo Commands

```bash
cargo new api-service          # create binary project
cargo build --release          # optimized build
cargo run                      # build + run
cargo test                     # run tests
cargo update                   # update dependency lock
cargo tree                     # dependency graph (requires cargo-tree)
cargo doc --open               # build docs locally
```

Install cargo tree (if missing):

```bash
cargo install cargo-tree
```

***

## 📦 Dependency Management

Dependencies declared in [`Cargo.toml`](/en/development-environment/supported-languages/rust/cargo.toml.md) under `[dependencies]`:

```toml
[dependencies]
reqwest = { version = "0.12", features = ["json"] }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
```

Update lockfile:

```bash
cargo update
```

***

## 🔄 Updating

```bash
rustup update            # update all toolchains
rustup self update       # update rustup itself
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.discloud.com/en/development-environment/local-environment/rust.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
