■ はじめに
以前、仕事で、プログラム言語の話になって、 Go言語が次にやりたいって話をしたところ、 Rustの方がいいのでは?的な話になったので、 気になって本を借りてみたら、かなり良さそうなのでメモ。 (本当は、仕事で使う Scala を勉強しないといけないのだが、 できれば、こっちの方、やりたい、、、)
目次
【0】Rust 1)クロスコンパイル 2)パフォーマンスの良さ 3)他言語との連携が用意 【1】環境構築 1)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] Microsoft C++ Build Toolsのインストール [2] Rustのインストール [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のインストール
* 以下のサイトから、ダウンロードしてインストールする
https://www.rust-lang.org/tools/install
[3] Visual Studio Code(VSCode)のインストール
* 以下の関連記事などを参考にインストールする
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
Visual Studio Code ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/20/230323