🦀

Rust 開発環境の構築

2022/07/02に公開

はじめに

本記事では、Linux (Ubuntu) / mac OS環境にRust言語の開発環境を構築するための手順をまとめています。Linuxもmacもどちらも同じ手順です。

インストール

インストールスクリプトがあるので、それを実行するだけで簡単です。

$ curl 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:
 ・・・
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
> 1

環境変数のセットアップ

今回はコマンドラインで実行しますが、毎回実行しなくても良いように自身のシェル環境に合わせて自動で実行されるようにしておきましょう。

$ source $HOME/.cargo/env

バージョン確認

$ rustc -V
rustc 1.45.0 (5c1f21c3b 2020-07-13)
$ cargo -V
cargo 1.45.0 (744bd1fbb 2020-06-15)

Hello, World (サンプルプログラム)

プロジェクト作成

$ cargo new hello

ビルド&実行

$ cd hello
$ cargo run

ビルドのみ

ビルドだけ行う場合は以下のコマンドで対応出来ます。デフォルトはdebugモードでのビルドみたいです。

$ cargo build

リリースモードでビルドする場合は、以下です。rustcコンパイラのビルドオプションが違うみたいですね。詳しくは以下を参照のこと。

https://qnighy.hatenablog.com/entry/2017/05/02/070000

$ cargo build --release

Rust環境のアップデート方法

コマンドラインでアップデート出来るので、定期的に実行してみましょう。

$ rustup update

VSCode Extensions for Rust

VSCodeのExtensionsとしては、rust-analyzerが良さそうです。

https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer

Discussion

ログインするとコメントできます