1
2

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の環境構築をした VSCode

Posted at

これは何?

いまさら,Rustに入門したのでやった環境構築とかメモしておきます。

GitHubのリポジトリ


install

curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.80.0
apt-get install -y --no-install-recommends gcc=4cargo --version:12.2.0-3 libc6-dev=2.36-9+deb12u8
source "$HOME/.cargo/env"

tools

rustup

  • rustのインストールやバージョン管理を行うことができるツール
  • 公式サイト
  • rustcやcargoなども一括でインストールされる。

rustc

rustのコンパイラ

cargo

  • rustのビルドシステム兼パッケージマネージャ
  • Cargo.tomlでプロジェクト管理する。

[!NOTE]
Rustではパッケージのことをクレート(Crate)と呼ぶ。

cargo new プロジェクト名
cargo clean
cargo check # コンパイルできるかチェックする
cargo build
cargo run

リリースに向けたビルド

--releaseをつけると最適化が行われ、コンパイル時間が長くなる代わりに実行速度が速くなる。

cargo build # target/debugに
cargo build --release # target/releaseに

パッケージの追加

  • Cargo.toml[dependencies]に追加する。
[package]
name = "chapter2"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.3" # ここに追加
  • cargo buildでライブラリが追加される。

[!NOTE]
Cargo.lockには依存関係のバージョンが記録される。

  • cargo updateを使うとCargo.lockを無視してCargo.tomlのバージョンを元に更新が走る。

コンパイラのエラーを直す

コードをコミットをせずに実行すると以下のようなエラーがでる。

cargo fix
error: the working directory of this package has uncommitted changes, and `cargo fix` can potentially perform destructive changes; if you'd like to suppress this error pass `--allow-dirty`, `--allow-staged`, or commit the changes to these files:
...

コードを修正してコミットするか、--allow-dirtyをつけて実行するか,コードをステージ(git add)して--allow-stagedをつけて実行するか選べるらしい。

cargo fix --allow-dirty

formatter

  • cargo fmtでコードをフォーマットすることができる。
rustup component add rustfmt # 一括ダウンロードの場合は不要
cargo fmt

linter

rustup component add clippy
cargo clippy

VS Codeの設定

以下をインストールする

  • rust-lang.rust-analyzer: Language Server
  • statiolake.vscode-rustfmt: Formatter
  • dustypomerleau.rust-syntax: Syntax Highlighting
  • vadimcn.vscode-lldb: Debugger
  • swellaby.vscode-rust-test-adapter: Test Runner
  • tamasfe.even-better-toml: TOML Highlighting
settings.json
  "[rust]": {
      "editor.defaultFormatter": "statiolake.vscode-rustfmt",
      "editor.formatOnSave": true,
      "editor.semanticHighlighting.enabled": false
  }

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?