概要

Rustは何かまだわかってないですが、社内でこの単語が出てきたことがあり、気になっていました。この機会にhello,worldしておきます。

環境

OS:

 

macOS Catalina

Version:
10.15.6

terminal
$ cargo –version
cargo 1.51.0 (43b129a20 2021-03-16)

$ rustc -V
rustc 1.51.0 (2fd73fabe 2021-03-23)

$ rustup -V
rustup 1.23.1 (3df2264a9 2020-11-30)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.51.0 (2fd73fabe 2021-03-23)`

$ rustup show
Default host: x86_64-apple-darwin
rustup home: /Users/xxx/.rustup

stable-x86_64-apple-darwin (default)
rustc 1.51.0 (2fd73fabe 2021-03-23)

ステップ1: Rustの導入

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

最初はこちらのページへアクセス。
結局のところ、
「curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh」
をターミナルで実行すれば良いらしい。

ステップ2: Rust用のファイルを作成

hello.rsを作成する。※公式サイトのhelloworldから引用
※作業場所はどこでもよい。以降のステップは同じディレクトリで作業。

hello.rs
fn main() {
println!(“Hello, World”);
}

ステップ3: Rustでコンパイル

Rustはコンパイルに、ステップ1の導入でインストールされたrustcを用いる。

terminal
$ rustc hello.rs

すると、エラーが。

terminal
error: linking with `cc` failed: exit code: 1
|
= note:

= note: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
error: aborting due to previous error

StackOverFlowに投稿されている記事を拝見すると、つまり↓ということらしい。

This is a very common problem on MacOS. You can fix it by typing xcode-select –install in terminal. It means developer tools are not installed.

ということで、下記を実行。XCodeのポップアップが出るので、インストールを進める。

terminal
$ xcode-select –install

(これがinstallされていないってことは、Macでコード書いてないことがバレる・・・?)
CommandLineToolsがインストールされた後、もう一度Rustコンパイルコマンドを実行!正常にコンパイルされた!

ステップ4: コンパイル後のバイナリファイルを実行

hello.rsをコンパイルするとhelloというバイナリファイルが作成されるはずなので、そちらを実行する。下記をターミナルで。

terminal
$ ./hello
Hello, World

所感

何か新しい言語の学習を始めるのってこんなに簡単だったっけ?
Rust公式サイトのリファレンスとかも充実してるし、hello,worldから順を追って勉強できるように用意してくれていて、学習がしやすい印象でした。(Vue.jsの勉強したときも同じ印象だったな)

Rustの旨味ってなんだ?ってところまで学習が繋げられると、業務でも活かせると思うので、そこまで続くといいな・・・。

bannerAds