最近RISC-Vに興味を持って勉強しており、関連資料・サイトを見ているとRust
という言語が出てくる。調べるほど魅力的な印象があったので、ついでにこの言語も勉強することにします。
Rust関連のリンク(ここみれば、ウチ含めてほかのサイトはいらない)
“プログラミング言語Rust: 2nd Edition”の日本語版PDFを作成した
https://qiita.com/yyu/items/18df35988f3ea3b12af0
Rustの環境構築 with VSCode
https://qiita.com/udayan28/items/e59afd39a7ab16911c25
Rust IDE に化ける VSCode
https://tech-blog.optim.co.jp/entry/2019/07/18/173000
必修言語Rustの他己紹介
https://tech-blog.optim.co.jp/entry/2019/01/07/173000
上記サイトから見つけた魅力的、興味が沸くこと
- VSCodeをRust IDE化してしまう方法
- システムライブラリに依存していない限りバイナリ単体で起動(つまりポータブル)
- RustはWindowsやLinux、Macといった一般的なプラットフォームだけでなくWebAssemblyを通してブラウザでも動く
- Raspberry PiやArduinoのような組み込み環境、あるいは最近話題のRISC-Vでも動作する
- RustはGCがなく、手動でメモリを管理するタイプの言語だが、ほとんどのメモリリークをコンパイルの時点で検出できる
デメリット
- 標準ライブラリが貧弱(必要なものはパッケージでダウンロードするので実質問題ない)
- エラー処理が面倒
- Rustのオブジェクト指向は継承しない。Javaで言う所のclassが無い、extendsが無い、interfaceが無い、implementsが無い。
- 関数は引数によるオーバーロードができない。
- 学習コストが高い
- Rustはエラーを気にしなければならなかったり変数の借用に厳密だったりと、雑に書くとコンパイラに怒られる(ある意味良いところだと思う)
WindowsにRUST開発環境を構築
エディタはVSCodeを利用する。
Install Rust
https://www.rust-lang.org/tools/install
rustup-init.exe
を実行すると以下のコンソールが立ち上がる(Win10だと警告が表示されるが続行)
%USERPROFILE%\.cargo\bin
This can be modified with the CARGO_HOME environment variable.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
%USERPROFILE%\.rustup
This can be modified with the RUSTUP_HOME environment variable.
This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-pc-windows-msvc
default toolchain: stable
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: latest update on 2019-11-07, rust version 1.39.0 (4560ea788 2019-11-04)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
180.7 MiB / 180.7 MiB (100 %) 69.2 MiB/s in 2s ETA: 0s
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
:
info: installing component 'rustfmt'
info: default toolchain set to 'stable'
stable installed - rustc 1.39.0 (4560ea788 2019-11-04)
Rust is installed now. Great!
To get started you need Cargo's bin directory (%USERPROFILE%.cargo\bin) in your
PATH
environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.
Press the Enter key to continue.
コマンドラインからちゃんと動作するか確認
> rustc --version
rustc 1.39.0 (4560ea788 2019-11-04)
> cargo --version
cargo 1.39.0 (1c6ec66d5 2019-09-30)
vscodeのエクステンションRust (rls)
をインストールする
cargoでrustのプロジェクトを作成する。指定した名前のサブフォルダが作成されて、gitリポジトリとメインのソースが一式作成される。
> cargo new hello
Created binary (application) `hello` package
> ls -a
./ ../ .git/ .gitignore Cargo.toml src/
> ls src
main.rs
Cargo.toml
はcargoコマンドのパッケージ管理ファイルで、npm
でいうところのpackage.jsonと同じようなものでしょう。
> cat Cargo.toml
[package]
name = "hello"
version = "0.1.0"
authors = ["hoge <hoge@com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
main.rs
を見るとC言語のメイン関数のようなものがあらかじめ記述されている。
> cat src/main.rs
fn main() {
println!("Hello, world!");
}
rustのビルドはcargo build
で実行する。ビルドする場所はプロジェクトフォルダの以下ならどこでも良さそう。ビルドすると、プロジェクト直下にtarget
フォルダといろんなモジュールが作成される。
--release
オプションを付けると、Releaseビルドになってtarget/release
以下に最適化されたモジュールが生成される。感覚的にわかりやすい。
> cargo build
Compiling hello v0.1.0 (C:\work\hello)
Finished dev [unoptimized + debuginfo] target(s) in 1.43s
> cd target/debug
> ls
build/ deps/ examples/ hello.d hello.exe* hello.pdb incremental/
cargo run
をやると、コンパイルとモジュールの実行が同時行われる
> cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\hello.exe`
Hello, world!
cargo check
またはcargo c
でコンパイル可能かを確認する。
> cargo check
Checking hello v0.1.0 (C:\work\hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.12s
RustをVSCodeでデバッグする
CodeLLDB
というデバッグツールをインストールすると可能になる
https://github.com/vadimcn/vscode-lldb/blob/v1.4.2/MANUAL.md
プロジェクトフォルダをVSCodeでオープンする。(直下にCargo.toml
があるところ)
デバッグのタブを開いて、歯車アイコンを選択すると、デバッグ環境でLLDB
を選択する。
Cargo.toml
を検出するので、そのままYesを選択
launch.jsonは以下のとおり
{
// 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 executable 'hello'",
"cargo": {
"args": [
"build",
"--bin=hello",
"--package=hello"
],
"filter": {
"name": "hello",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'hello'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=hello",
"--package=hello"
],
"filter": {
"name": "hello",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
ソースにブレークポイントを設定して、
デバッグを実行する。。が、以下のようなエラーが出た。
よく見ると、Python云々のエラーが出ている(またPythonか。。)筆者の環境ではAnaconda3を使っており、コマンドプロンプト上だとPythonまでのパスが利いていないのがいけないのかもしれない。
Listening on port 50092
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00001920 (most recent call first):
Debug adapter exit code=3221226505, signal=null.
とりあえず、環境変数PYTHONHOMEにAnaconda3のルートフォルダ(直下にpython.exeがある)を指定した状態でVSCodeを起動してデバッグを実行すると動くようになった。
SET PYTHONHOME=%USERPROFILE%\Anaconda3
code
いろいろなアプリで使っていくために
ざっと見た感じRust向けのパッケージがたくさん公開されているので大方大丈夫そうではある。だけどまだ日が浅い言語なので、いきなり実践で使うにはちょっと不安でもある。
- 画像処理系(opencvのラッパー?一応ある)
- HTTPサーバー関連(Node.jsのExpressみたいなものあるのかな?)
- Socket.IO関連( socket_io::socketかな? )
- USB, Bluetooth関連
まずはこのあたりが使えるのか?またはどうやって使うのかを今後検証していきたい。