> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-21-development-environments/di-21.5-jie-rust-kai-fa-huan-jing.md).

# 21.5 Rust Development Environment

This section introduces how to configure a Rust development environment (via rustup or pkg) on FreeBSD, including toolchain installation and version settings.

Rust is designed with memory safety and thread safety as core goals, and concurrent programming is also one of its important features.

## Installing Rust

Install Rust using pkg:

```sh
# pkg install rust
```

Or install using Ports:

```sh
# cd /usr/ports/lang/rust/
# make install clean
```

> **Tip**
>
> The latest development version is available in Port `lang/rust-nightly`.

> **Note**
>
> The Rust version number `1.95.0` in this section is the Ports version at the time of writing. Please adjust according to the latest Rust version available in FreeBSD Ports, which can be queried with `pkg search rust`.

After successful installation, check the software version.

Display the Rust compiler version information:

```sh
$ rustc --version
rustc 1.95.0 (59807616e 2026-04-14) (built from a source tarball)
```

Display the Cargo (Rust package manager and build tool) version information:

```sh
$ cargo --version
cargo 1.95.0 (f2d3ce0bd 2026-03-21) (built from a source tarball)
```

## A Blessing for a Beautiful World

Create a new project using cargo:

```sh
$ cargo new ~/projects/greeting
    Creating binary (application) `greeting` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
```

Enter the newly created project:

```sh
$ cd $_
```

Edit the text file `~/projects/greeting/src/main.rs`, ensuring it contains the following code (usually included by default when creating a new project):

```rust
fn main() {                               // Rust program entry function
    println!("Hello, world!");            // Print "Hello, world!" to the console
}
```

After saving, run:

```sh
$ cargo run
   Compiling greeting v0.1.0 (/home/ykla/projects/greeting)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
     Running `target/debug/greeting`
Hello, world!
```

Compile and run the current Rust project.

## JetBrains Rust Development Environment

Install using pkg:

```sh
# pkg install jetbrains-rustrover
```

Or install using Ports:

```sh
# cd /usr/ports/devel/jetbrains-rustrover/
# make install clean
```

![JetBrains Rust development environment interface example](/files/E3Vxmusc20v4ZxWhAnFl)

## Appendix: Managing Rust via rustup

rustup-init is used to install and initialize rustup as a regular or non-root user.

rustup is the official Rust version management tool. It can install the Rust programming language from official channels and manage and switch between different Rust versions, eliminating the need to rely on Ports' lang/rust and `lang/rust-nightly`. Similar to version managers for other programming languages, rustup provides convenient access to and management of various Rust versions.

### Installing rustup

Install using pkg:

```sh
# pkg install rustup-init
```

Install using Ports:

```sh
# cd /usr/ports/devel/rustup-init/
# make install clean
```

### Managing Rust

Initialize the rustup management tool:

```sh
$ rustup-init --profile minimal --default-toolchain none -y
```

The output is similar to the following:

```sh
info: profile set to 'minimal'
info: default host triple is x86_64-unknown-freebsd
info: skipping toolchain installation


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
source "$HOME/.cargo/env.nu"    # For nushell
```

Follow the prompts to write the environment variables to the corresponding configuration file for your shell. For example, for sh, add the following to `~/.shrc`:

```sh
. $HOME/.cargo/env
```

> **Warning**
>
> Note the leading dot (`.`) in the command, otherwise you will get a "permission denied" error.

Reload the shell configuration:

```sh
$ . ~/.shrc
```

Install the latest Rust stable toolchain and set it as the system default compiler.

```sh
$ rustup default stable
```

The output is as follows:

```sh
info: syncing channel updates for 'stable-x86_64-unknown-freebsd'
info: latest update on 2026-04-16, rust version 1.95.0 (59807616e 2026-04-14)
info: downloading component 'cargo'
 11.1 MiB /  11.1 MiB (100 %)   5.4 MiB/s in  3s
info: downloading component 'rust-std'
 27.5 MiB /  27.5 MiB (100 %)   7.6 MiB/s in  3s
info: downloading component 'rustc'
 88.4 MiB /  88.4 MiB (100 %)  15.1 MiB/s in 32s
info: installing component 'cargo'
info: installing component 'rust-std'
 27.5 MiB /  27.5 MiB (100 %)  11.7 MiB/s in  2s
info: installing component 'rustc'
 88.4 MiB /  88.4 MiB (100 %)  13.4 MiB/s in 37s
info: default toolchain set to 'stable-x86_64-unknown-freebsd'

  stable-x86_64-unknown-freebsd installed - rustc 1.95.0 (59807616e 2026-04-14)
```

## References

* Rust Project. The Rust Programming Language\[EB/OL]. \[2026-04-17]. <https://doc.rust-lang.org/book/>. The official Rust programming guide, covering core concepts such as ownership and lifetimes.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://book.bsdcn.org/ask/flat/chapter-21-development-environments/di-21.5-jie-rust-kai-fa-huan-jing.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.
