【Rust】Rust ~ 環境構築編 ~

■ はじめに

以前、仕事で、プログラム言語の話になって、
Go言語が次にやりたいって話をしたところ、
Rustの方がいいのでは?的な話になったので、
気になって本を借りてみたら、かなり良さそうなのでメモ。

(本当は、仕事で使う Scala を勉強しないといけないのだが、
できれば、こっちの方、やりたい、、、)

目次

【0】Rust
 1)クロスコンパイル
 2)パフォーマンスの良さ
 3)他言語との連携が用意
【1】環境構築
 1)Windows / 簡易版
 2)Windows / 以前やったやり方
【2】Hello world
 1)プロジェクト作成
 2)プログラム修正
 3)実行
【3】トラブルシュート
 1)エラー「unable to find executable for <your program>.exe」が表示

【0】Rust

* 以下の利点があるプログラム言語、、、
 => めちゃめちゃいいとこどりだと思った。。。

1)クロスコンパイル

* Windows/Linux/MacOS で使える
 => Javaなどと同じ。
 => でも最近の言語では別に珍しくはない。

2)パフォーマンスの良さ

* C++並みに早い
 => Java/Swift/Go より 2~3倍速い
 => Ruby/Python3 より 約30倍速い

理由

* 機械語にコンパイル
* GCがない(「所有権」という独自の概念により)
* 静的型付け
* ゼロコスト抽象化 (Zero-cost abstraction)

3)他言語との連携が用意

* 他言語関数インターフェイス(Foreign Function Interface; FFI)
説明
rustc Rustコンパイラ
rustup Rustインストーラ・アップデータ
cargo Rustパッケージマネージャ
rustdoc ドキュメント生成ツール(rustdoc)
rustfmt コーディングスタイル整形ツール

【1】環境構築

1)Windows / 簡易版

[1] Rustのインストール

[1] Rustのインストール

* 以下のサイトから、ダウンロードしてインストールする
 => 「1」「1」(※)と選んでいけば、
  Visual Studioが入って、いい感じで構築できる
 (ただし、5~6GBの空きが必要)

https://www.rust-lang.org/tools/install

1) Quick install via the Visual Studio Community installer
   (free for individuals, academic uses, and open source).
2) Manually install the prerequisites
   (for enterprise and advanced users).
3) Don't install the prerequisites
   (if you're targeting the GNU ABI).
>1 (「1」を選択)

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1 (「1」を選択)

2)Windows / 以前やったやり方

[1] Microsoft C++ Build Toolsのインストール
[2] Rust (rustup) のインストール
[3] Visual Studio Code(VSCode)のインストール
 3-1) 拡張機能「rust-analyzer」のインストール
 3-2) 拡張機能「CodeLLDB」のインストール

https://developer.mamezou-tech.com/blogs/2023/02/12/using-rust-01/

[1] Microsoft C++ Build Toolsのインストール

* 以下のサイトから、ダウンロードしてインストールする
 => 「C++によるデスクトップ開発」にチェックを付ける
 => [言語パック]タブを選択し[日本語][英語]にチェックを付ける
 => 「インストール」ボタン押下し終わったらOS再起動

https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/

[2] Rust (rustup) のインストール

* 以下のサイトから、ダウンロードしてインストールする
 => 終わったら、コマンドプロンプトで
  「rustup -V」「cargo --version」し確認

https://www.rust-lang.org/tools/install
補足:rustup

* rustをアップデートする (「rustup update」)
* 「rustc (コンパイラ)」「cargo (パッケージマネージャ)」が同梱

[3] Visual Studio CodeVSCode)のインストール

* 以下の関連記事などを参考にインストールする

https://dk521123.hatenablog.com/entry/2019/10/20/230323

3-1) 拡張機能「rust-analyzer」のインストール

* VS Code を開き、拡張機能メニュー (Ctrl + Shift + X) で
 『rust-analyzer』を検索し、インストールする

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

3-2) 拡張機能「CodeLLDB」のインストール

* VS Code を開き、拡張機能メニュー (Ctrl + Shift + X) で
 『CodeLLDB』を検索し、インストールする

【2】Hello world

1)プロジェクト作成

* 「cargo new <プロジェクト名>」でプロジェクト作成

コマンド例

# cargo new <プロジェクト名>
cargo new first_rust_project

# cf. CargoはRustのビルドシステム兼パッケージマネージャ

実行後のフォルダ構成

├─.vscode
│   └─ launch.json
└─first_rust_project ... プロジェクト
    ├─src
    │  └─ main.rs ... メイン関数
    └─target

2)プログラム修正

* VS Code で ./first_rust_project/src/main.rs を修正してみる

修正例

fn main() {
    println!("Hello, world!!!?");
}

3)実行

* VS Code の ./first_rust_project/src/main.rs 内に表示されている
 「run」をクリックし、実行
 => 実行時にエラーが表示される場合、「【3】トラブルシュート」を参照

実行例

 *  Executing task: C:\Users\user\.cargo\bin\cargo.exe run --package first_rust_project --bin first_rust_project 

   Compiling first_rust_project v0.1.0 (C:\xxx\xxxx\first_rust_project)
    Finished dev [unoptimized + debuginfo] target(s) in 0.74s
     Running `target\debug\first_rust_project.exe`
Hello, world!!!?
 *  Terminal will be reused by tasks, press any key to close it.

【3】トラブルシュート

1)エラー「unable to find executable for .exe」が表示

 実行しようとしたら
エラー「unable to find executable for <your program>.exe」
が表示されてしまう

解決案

* .vscode/launch.json を以下のように
 「"program": "${workspaceFolder}/【プロジェクト名】/src/main"」
 とし、再実行

修正例

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug",
      "program": "${workspaceFolder}/first_rust_project/src/main",
      "args": [],
      "cwd": "${workspaceFolder}"
    }
  ]
}

参考文献

https://learn.microsoft.com/ja-jp/windows/dev-environment/rust/setup
https://developer.mamezou-tech.com/blogs/2023/02/12/using-rust-01/

関連記事

Rust ~ 基本編 / 保有権 ~
https://dk521123.hatenablog.com/entry/2023/05/04/213726
Rust ~ axum / 入門編 ~
https://dk521123.hatenablog.com/entry/2023/09/02/224707
Visual Studio Code ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/20/230323
sqlparser-rs ~ SQL Parser for Rust ~
https://dk521123.hatenablog.com/entry/2023/06/12/000000