0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rust 学習 環境構築

Posted at

使用する環境

Machine        : MacBook Air
Chip           : Apple M1
OS version     : MacOS Sequoia 15.5

公式サイト

$ rustup vs $ brew

環境が macOS のため Homebrew を利用しようと思ったが、学習のために公式サイトで説明されている rustup を利用することにした。

The first step is to install Rust. We’ll download Rust through rustup, a command line tool for managing Rust versions and associated tools.

Homebrew でインストールする場合
$ brew search rust
==> Formulae
c2rust               languagetool-rust    rust-parallel        rustcat              rustscan             shadowsocks-rust     virustotal-cli
choose-rust          rust                 rust-script          rustic               rustup               starlark-rust        rush
gerust               rust-analyzer        rustc-completion     rustls-ffi           rustywind            uncrustify

==> Casks
font-moderustic                     rustdesk                            rustrover                           uncrustifyx

If you meant "rust" specifically:
It was migrated from homebrew/cask to homebrew/core.

$ brew install rust

インストール

公式サイトで説明されている通り、下記のコマンドを実行。

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

version 1.88.0 がインストールされた様子。

インストール時のメッセージを見ると、環境変数の設定のために env ファイルの実行が必要とのことだった。

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

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 $"($nu.home-path)/.cargo/env.nu"  # For nushell

使っているシェルは Bash なので、下記を実行する。

$ . "$HOME/.cargo/env"

環境変数 $PATH$HOME/.cargo/bin が追加されていた。

env 実行前
$ echo $PATH
...

$ . "$HOME/.cargo/env"

$ echo $PATH
/Users/ユーザ名/.cargo/bin:...
実行結果
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /Users/ユーザ名/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /Users/ユーザ名/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /Users/ユーザ名/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /Users/ユーザ名/.profile
  /Users/ユーザ名/.bash_profile
  /Users/ユーザ名/.bashrc
  /Users/ユーザ名/.zshenv

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: aarch64-apple-darwin
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>

info: profile set to 'default'
info: default host triple is aarch64-apple-darwin
info: syncing channel updates for 'stable-aarch64-apple-darwin'
info: latest update on 2025-06-26, rust version 1.88.0 (6b00bc388 2025-06-23)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 60.4 MiB /  60.4 MiB (100 %)  19.0 MiB/s in  3s         
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 20.1 MiB /  20.1 MiB (100 %)   4.1 MiB/s in  2s         
info: installing component 'rust-std'
 25.0 MiB /  25.0 MiB (100 %)  18.7 MiB/s in  1s         
info: installing component 'rustc'
 60.4 MiB /  60.4 MiB (100 %)  20.7 MiB/s in  2s         
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-apple-darwin'

  stable-aarch64-apple-darwin installed - rustc 1.88.0 (6b00bc388 2025-06-23)


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 $"($nu.home-path)/.cargo/env.nu"  # For nushell

バージョンを確認する

インストールできたかを確認する。

Rust のバージョンを確認する
$ rustc --version
rustc 1.88.0 (6b00bc388 2025-06-23)

ローカルのドキュメント

Rust をインストールすると、ローカル環境にドキュメントもダウンロードされる。

ドキュメントをブラウザで開く
$ rustup doc

Screenshot 2025-07-03 at 6.09.40.png

アップデート

Rust をアップデートする
$ rustup update

アンインストール

インストール時に、アンインストール配管コマンドを実行すれば良いとあった。

$ rustup self uninstall

Hello world

VSCode に Rust の拡張機能を追加しておく。

Screenshot 2025-07-03 at 6.25.58.png

Screenshot 2025-07-03 at 6.26.10.png

公式サイトに従ってプロジェクトディレクトリを作成する。

$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world

Rust ファイルは、拡張子 rs を持ち、アンダースコア _ を使用する。

  • hello_world.rs

main.rs を作成する

~/projects/hello_world/main.rs
fn main() {
    println!("Hello, world!");
}
コンパイルして実行する
$ cd ~/projects
$ cd hello_world
$ rustc main.rs   # コンパイル
$ ./main          # 実行
Hello, world!

$ rustup

Rust ツールチェインの管理ツール。

複数バージョンの Rust をインストールしたり、切り替えたりすることができる。

$ rustc

Rust のコンパイラ。

Rust のソースコード(.rs ファイル)をバイナリ(実行可能ファイル)にコンパイルすることができる。

ソースファイルをコンパイルする
$ rustc ソースファイル

$ cargo

Rust パッケージマネージャ兼ビルドツール。

ビルド、依存関係の管理、テスト、ドキュメント生成などのほとんどすべての作業を $ cargo で行うことができる。

内部で $ rustc を使用している。

新規プロジェクトを作成する
$ cargo new プロジェクト名
ビルドする
$ cargo build               # 開発用ビルド。最適化は行われない。
$ cargo build --release     # リリース用ビルド。最適化が行われる。
ビルドして、実行まで行う
$ cargo run
コンパイルチェックする
$ cargo check
テストのを実行する
$ cargo test
依存ライブラリを最新に更新する
$ cargo update
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?