わりと新しいRustをお試しで触ってみる

技術ネタ
この記事は約10分で読めます。

こんばんわ、hisayukiです。

最近AWSがRustという言語を支援するという記事がでました。

「Rust」言語を採用したAWS、Rustプロジェクト支援を開始
AWSは、オープンソースのシステムプログラミング言語「Rust」の採用に続いて、開発プロジェクトをスポンサーとして支援することを発表した。

果たしてどんな言語なのかよくわかってないです(;´∀`)
ただ、AWSがサービス内で使っているとのことなので未来への期待値が高いのでしょう。
実際SDKが出てたり、Lambdaでの利用を推奨してるので

というわけで、実際に触ってみようと思ったのでやってみました!

スポンサーリンク

使えるようになるまで

ここ見ながらインストールできるのですが、英語だらけで辛いって方向けに書いていきます。
今回はMacでインストールするので、Windowsの方はごめんなさい(´;ω;`)

結局やったこと

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
~~~~~
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
~~~~
$ source ~/.profile

こんだけです。
インストール超簡単で速攻でおわりますが、一応このあとに詳しい事書いておきます。

インストール

まずはcurlで以下のコマンドながします。

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

コマンド流すとこんな感じになります。

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:

  /Users/hisayuki/.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:

  /Users/hisayuki/.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 profile files located at:

  /Users/hisayuki/.profile
/Users/hisayuki/.bash_profile

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-apple-darwin
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>

ユーザー名
まずは最初なので1.Proceed with installation (default)を選びます。

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-apple-darwin'
info: latest update on 2019-09-26, rust version 1.38.0 (625451e37 2019-09-23)
info: downloading component 'cargo'
  3.7 MiB /   3.7 MiB (100 %)   2.7 MiB/s in  1s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 11.7 MiB /  11.7 MiB (100 %)   2.7 MiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
170.9 MiB / 170.9 MiB (100 %)   2.9 MiB/s in  1m  4s ETA:  0s
info: downloading component 'rustc'
 60.4 MiB /  60.4 MiB (100 %)   2.4 MiB/s in 21s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 11.7 MiB /  11.7 MiB (100 %)   5.1 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
170.9 MiB / 170.9 MiB (100 %)  28.6 MiB/s in  6s ETA:  0s
info: installing component 'rustc'
 60.4 MiB /  60.4 MiB (100 %)  12.0 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.38.0 (625451e37 2019-09-23)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.

インストール自体はこれで完了となります。

Pathを通す

インストールは終わりましたが、最初のcurlコマンドを流したあとの英文でこんなのがあります。

It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:

  /Users/hisayuki/.cargo/bin

This can be modified with the CARGO_HOME environment variable.

ざっくり何が書いてあるかと言うと、

コマンドcargo, rustc, rustup/Users/ユーザー名/.cargo/binにあります。
このパスは環境変数CARGO_HOMEに設定します。

って書いてあります。もう一つの英文も

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /Users/hisayuki/.rustup

This can be modified with the RUSTUP_HOME environment variable.

Rustupのメタデータとツールチェーンは/Users/ユーザー名/.cargo/binにあります。
このパスは環境変数
RUSTUP_HOMEに設定します。

両方ともPATHに設定加えないといけないっぽい。
今回はとりあえずはコマンドの方だけ設定します。

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /Users/hisayuki/.profile
/Users/hisayuki/.bash_profile

このパスはPATH環境変数に追加されます、次の場所にあるプロファイルファイルを変更します。

/Users/hisayuki/.profile
/Users/hisayuki/.bash_profile

.bash_profileでもよいのですが、rustのインストール時に.profileというファイルが作成されます。
作られるということはこっちに書いたほうがいいのかなと思い開きました。

export PATH="$HOME/.cargo/bin:$PATH"

書いてあるやん。

というわけで、source ~/.profileコマンドで読み込ませたらコマンド動くようになりました。

プロジェクトを作ってみる

インストール終わったので、さっそくプロジェクトを作ります。
今回はrust_sampleという名前でプロジェクトを作ります。

$ cargo new rust_sample
     Created binary (application) `rust_sample` package

出来上がったプロジェクトに移動して、中を見てみます。

$ cd rust_sample
$ ls -la
total 16
drwxr-xr-x   6 hisayuki  staff  192 10 26 12:36 .
drwxr-xr-x@ 23 hisayuki  staff  736 10 26 12:36 ..
drwxr-xr-x  10 hisayuki  staff  320 10 26 12:36 .git
-rw-r--r--   1 hisayuki  staff   19 10 26 12:36 .gitignore
-rw-r--r--   1 hisayuki  staff  226 10 26 12:36 Cargo.toml
drwxr-xr-x   3 hisayuki  staff   96 10 26 12:36 src

git標準搭載でプロジェクトが出来上がってます。
しかも、cargo runコマンドで既にこの状態で実行できるようになってます!

$ cargo run
   Compiling rust_sample v0.1.0 (/Users/hisayuki/vscode/rust_sample)
    Finished dev [unoptimized + debuginfo] target(s) in 3.81s
     Running `target/debug/rust_sample`
Hello, world!

もはや試すのに、Hello, world!すら入力する必要ないです。

チュートリアルが日本語

公式サイトはすべて英語ですが、日本語チュートリアルが存在してます!

まえがき - The Rust Programming Language

翻訳してくれてる人がいるってことですね、これ本当にありがたいです。
あと、Slackの日本コミュニティもあるみたいです!

Offline for Maintenance

公式プラグイン

公式サイトにていくつかプラグインも紹介されています。

普段からVSCodeを使っているので、VSCodeのプラグインをインストールしました。

Rust (deprecated) - Visual Studio Marketplace
Extension for Visual Studio Code - Rust for Visual Studio Code (powered by Rust Language Server/Rust Analyzer). Provides lints, code completion and navigation, ...

まとめ

今回はインストールして、まず動かすところまで書きました!
いろいろ情報見てると、一回で全部書ききるのは難しいなぁと・・・w

なので実際にいろいろ書いてみてからの感想はまた別で書こうと思います!

コメント

  1. .profileはsh(bsh)用、.bash_profileはbash用のプロファイルです
    昨今のUnix系ではshとbashは区別がないのですが一応区別されます。

    あとはRustといえばちょっと前にMozillaが採用してましたね。
    なぜ最近になって脚光を浴びだしたのか謎ですが。

  2. コメントありがとうございます!
    そういうことなんですね、shとbashが区別あまり付いてなかったですが
    最近、alpineなどの軽量OS使うとbashは入ってないので自分で入れるものなんだなーって気づきましたw

    RustはAWSが去年Lambdaに対応したことで、僕は知りました!

  3. Windows版・・・・