7
9

More than 1 year has passed since last update.

MacでRustの開発環境を構築する

Last updated at Posted at 2023-08-20

rustupかHomebrewか

Rustをセットアップするには、rustupというRustのセットアップコマンドを使用するのがお勧めです。
Homebrewでrustc(rustコンパイラ)をインストールする事はできますが、開発に必要な各種コマンド(linterのclippyなど)がHomebrewに無く、それらのインストールに多大な労力が必要になるのでお勧めできません。

rustupのインストール

まず、rustupをインストールします。
rustupのインストール方法には、Homebrewを使う方法と、セットアップコマンドを使う方法があります。
どちらもそれほど手間は変わらないですが、Homebrewがセットアップ済みでしたら、Homebrewでrustupをインストールすると良いでしょう。

Homebrew でインストールする場合

% brew install rustup-init
% rustup-init

インストールコマンドを使う場合

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

rustup-initを実行すると、下記のようなメッセージが出ます。

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

  /Users/sakura/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /Users/sakura/.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/sakura/.cargo/bin

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

  /Users/sakura/.profile
  /Users/sakura/.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 installation (default)
2) Customize installation
3) Cancel installation
>      

インストールパスやアーキテクチャの説明が表示されますので、確認してください。
入力待ちの状態になるのでデフォルト動作のinstallを実行するため、エンターを押します。。

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 2023-08-03, rust version 1.71.1 (eb26296b5 2023-08-03)
info: downloading component 'cargo'
  5.0 MiB /   5.0 MiB (100 %)   4.3 MiB/s in  1s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 13.6 MiB /  13.6 MiB (100 %)  11.8 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 23.7 MiB /  23.7 MiB (100 %)   5.1 MiB/s in  4s ETA:  0s
info: downloading component 'rustc'
 52.6 MiB /  52.6 MiB (100 %)   7.8 MiB/s in  7s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 13.6 MiB /  13.6 MiB (100 %)   5.5 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
 23.7 MiB /  23.7 MiB (100 %)  19.1 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 52.6 MiB /  52.6 MiB (100 %)  21.3 MiB/s in  2s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-apple-darwin'

  stable-aarch64-apple-darwin installed - rustc 1.71.1 (eb26296b5 2023-08-03)


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, run:
source "$HOME/.cargo/env"

このようになればインストールが終了します。

この状態では、今開いているterminalには、パスの情報が反映されていないため、rustを実行できません。
次のステップに行くには、terminalを再起動するか、 出力メッセージあるようにsource "$HOME/.cargo/env" を実行します。

rustがインストールされているか確認しましょう。rustup --versionと打って下記のようになればインストールは成功です。

% rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.58.1 (db9d1b20b 2022-01-20)`

rust を使ってみる

cargoを使ってサンプルプロジェクトをダウンロードしてきます。

% cargo new hello_rust
     Created binary (application) `hello_rust` package

ダウンロードしたプロジェクトのディレクトリに移動します。

% cd hello_rust

プロジェクトをコンパイルして実行します。

% cargo run
   Compiling hello_rust v0.1.0 (/Users/sakura/Downloads/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.35s
     Running `target/debug/hello_rust`
Hello, world!

Hello, world! と表示されれば成功です。

Visual Studio Codeで使う

Visual Studio Codeの拡張期のrust-analyzer をインストールします。

まず、rust-analyzerに必要なコマンドをrustupコマンドでインストールします。

% rustup component add rls rust-src rust-analysis
info: downloading component 'rls'
info: installing component 'rls'
info: downloading component 'rust-src'
info: installing component 'rust-src'
info: downloading component 'rust-analysis'
info: installing component 'rust-analysis'

次にVisual Studio Codeから、rust-analyzerの拡張機のをインストールします。
image.png
これでVisual Studio Codeでrustを使う準備は完了です。

先ほどcargoでダウンロードしたhello_rustのプロジェクトをVisual Studio Codeで開いてみましょう。
srcのディレクトリの中にmain.rsというファイルがあります。これがrustのソースコードファイルになります。これを開いてもらうと、このように色分けされて表示されます。
image.png

#参考
https://chicog.me/posts/6hs4-m7dac93
https://zenn.dev/shichi18/articles/20230409-01-52519cc3cda868
https://zenn.dev/23prime/articles/74cda5a096a3b3
https://qiita.com/notakaos/items/9f3ee8a3f3a0caf39f7b

7
9
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
7
9