見出し画像

Rustはじめました

はじめに

「Rustとはなにか」については、他の記事やリソースに譲るとして、私がこの言語に惹かれたのは、何と言っても「安全なプログラムを書いてもパフォーマンスが落ちない」という評判だ。

C++11でラムダ式を使った非同期プログラムを書いていたら、渡したはずの変数の参照先がないという、わかってみれば初歩的なミスで頭を悩ませたことがあった。もうあんなことはまっぴらごめんだ。C/C++は自由度が非常に高いが、その責任は自分に返ってくる。とは言え、言語側が持つべき責任がお役所仕事では困る。

一方で、安全性の高い言語も数多くある。JavaScriptやPython等の言語では、「ガベージコレクション」でデータを破棄する時期を管理しているので、「参照先がない」なんてことはない。一方で処理速度を犠牲にしていると言われる。私は仕事でJS/TSをよく利用しているが、あまり早さを気にしたことがない(Web上のリソースへのアクセスの方がはるかに遅いからか?)。なので、ここではその評判を鵜呑みするとする。

曰く、Rustは「安全が保証された高速なプログラム」を書ける言語なのだそうだ。夢のような言語だな、こりゃ。そういうわけで、私はこの言語を身につけることにした。

Windowsにインストール

まずはRustをWindowsにインストールしてみる。

Windows 64bitの rustup-init.exe をダウンロードしてコマンドラインで起動する。

# Rustインストーラ
>rustup-init

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:

  C:\Users\XXX\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  C:\Users\XXX\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  C:\Users\XXX\.cargo\bin

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 (default)
               profile: default
  modify PATH variable: yes

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

info: profile set to 'default'
info: default host triple is x86_64-pc-windows-msvc
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: latest update on 2023-03-09, rust version 1.68.0 (2c8cc3432 2023-03-06)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 19.4 MiB /  19.4 MiB (100 %)   9.4 MiB/s in  2s ETA:  0s
info: downloading component 'rust-std'
 27.6 MiB /  27.6 MiB (100 %)  13.2 MiB/s in  2s ETA:  0s
info: downloading component 'rustc'
 63.9 MiB /  63.9 MiB (100 %)  14.0 MiB/s in  4s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.4 MiB /  19.4 MiB (100 %)   1.5 MiB/s in 23s ETA:  0s
info: installing component 'rust-std'
 27.6 MiB /  27.6 MiB (100 %)   7.2 MiB/s in  3s ETA:  0s
info: installing component 'rustc'
 63.9 MiB /  63.9 MiB (100 %)   8.3 MiB/s in  7s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'

  stable-x86_64-pc-windows-msvc installed - rustc 1.68.0 (2c8cc3432 2023-03-06)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (%USERPROFILE%\.cargo\bin).

Press the Enter key to continue.

>

このままでは、rustup/cargo/rustc/rustdocなどのコマンドは動いてくれない。メッセージにあるように、一度コマンドプロンプトを閉じて、再度コマンドプロンプトを開き、変更されたPATH環境変数を有効にする。

# rustupコマンドのバージョン
>rustup --version
rustup 1.25.2 (17db695f1 2023-02-01)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.68.0 (2c8cc3432 2023-03-06)`

# cargoコマンドのバージョン
>cargo --version
cargo 1.68.0 (115f34552 2023-02-26)

# rustcコマンドのバージョン
>rustc --version
rustc 1.68.0 (2c8cc3432 2023-03-06)

# rustdocコマンドのバージョン
>rustdoc --version
rustdoc 1.68.0 (2c8cc3432 2023-03-06)

PC?コンパイラ?64 or 32?

ちなみに、私の環境では、この状態でRustのプログラムを作ると、MSVCコンパイラを使ったWindows 64bitアプリケーションができる。おそらくインストール時に自動検出されたであろうMSVCをみて、stable-x86_64-pc-windows-msvc という「ツールチェーン」が採用されたと思われる。

ただし、私の都合でWindows 32bitアプリも作りたい。この場合、rustupを使って新しいツールチェーンをインストールすることになる。

# 念のため最新の状態かチェック
>rustup update
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: checking for self-updates

  stable-x86_64-pc-windows-msvc unchanged - rustc 1.68.0 (2c8cc3432 2023-03-06)

info: cleaning up downloads & tmp directories

# 32bit Windows MSVCツールチェーンをインストール
>rustup toolchain install stable-i686-pc-windows-msvc
info: syncing channel updates for 'stable-i686-pc-windows-msvc'
info: latest update on 2023-03-09, rust version 1.68.0 (2c8cc3432 2023-03-06)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.3 MiB /  19.3 MiB (100 %)   1.5 MiB/s in 21s ETA:  0s
info: installing component 'rust-std'
 27.3 MiB /  27.3 MiB (100 %)   6.9 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 57.9 MiB /  57.9 MiB (100 %)   8.8 MiB/s in  6s ETA:  0s
info: installing component 'rustfmt'

  stable-i686-pc-windows-msvc installed - rustc 1.68.0 (2c8cc3432 2023-03-06)

info: checking for self-updates

# 状態を確認
>rustup show
Default host: x86_64-pc-windows-msvc
rustup home:  C:\Users\XXX\.rustup

installed toolchains
--------------------

stable-i686-pc-windows-msvc
stable-x86_64-pc-windows-msvc (default)

active toolchain
----------------

stable-x86_64-pc-windows-msvc (default)
rustc 1.68.0 (2c8cc3432 2023-03-06)

デフォルトは64bitのままだが、32bitツールチェーンも追加されている。このままでcargoコマンドを実行すると64bitツールチェーンでコンパイルされる。コンパイル時にツールチェーンを変更するには--targetオプションを使う。

まとめ

新しいことを始めると、まあまあ早めにお腹いっぱいになる。ここまでさらっと書いたが、実は結構、試行錯誤してここまで来ている。頭が破綻しないうちに切り上げて、ここまで学んだことを反芻しよう。

この記事が気に入ったらサポートをしてみませんか?