#はじめに
なんちゃってプログラマーがRustを触ってみた備忘録です。
#検証環境
- Mac -> Vagrant -> CentOS7
#インストール
以下のサイトからコマンドを取得します。
# インストールコマンド
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 途中でインストール方法を確認されるのでdefaultを選択
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
インストール完了後にバージョン確認してみます。
# バージョン確認
$ cargo --version
-bash: cargo: command not found
$ rustc -V
-bash: rustc: command not found
$ rustup -V
-bash: rustup: command not found
PATHを通すにはログアウトが必要そうです。
プラットフォームやコマンドシェルの差異、あるいはrustupのバグにより、PATHの変更はコンソールを再起動、もしくはユーザーがログアウトするまで適用されない場合や、完全に失敗してしまう場合があります。
出典:https://www.rust-lang.org/ja/tools/install
一度入り直してから再度バージョン確認してみます。
$ cargo --version
cargo 1.54.0 (5ae8d74b3 2021-06-22)
$ rustc -V
rustc 1.54.0 (a178d0322 2021-07-26)
$ rustup -V
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.54.0 (a178d0322 2021-07-26)`
#Hello World!の表示
RustはHello World!用のソースを自前で用意しています。
# プロジェクトを作成
$ cargo new sample-project
Created binary (application) `sample-project` package
# プロジェクト用のディレクトリが作成
$ ls -latr | grep sample-project
drwxrwxr-x. 4 vagrant vagrant 65 Aug 13 08:30 sample-project
# 作成されたプロジェクトのディレクトリに入る
$ cd sample-project/
# 実行
$ cargo run
Compiling sample-project v0.1.0 (/home/vagrant/sample-project)
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: aborting due to previous error
error: could not compile `sample-project`
To learn more, run the command again with --verbose.
cc(gcc)というパッケージが入っていないよ、と怒られています。
なので該当パッケージをインストールして再挑戦します。
# 必要なパッケージをインストール
$ sudo yum install -y gcc
# 再実行
$ cargo run
Compiling sample-project v0.1.0 (/home/vagrant/sample-project)
Finished dev [unoptimized + debuginfo] target(s) in 1.49s
Running `target/debug/sample-project`
Hello, world!
Hello World!を書かなくてもいい時代が来るとは感慨深いものです。
#小さいRustアプリケーションの開発
Hello Worldをもう少しおしゃれに表示させます。
# 依存関係を記入するファイル
$ ls -latr | grep Cargo.toml
-rw-rw-r--. 1 vagrant vagrant 183 8月 13 08:30 Cargo.toml
# dependenciesにferris-says = "0.2"を追加
$ vi Cargo.toml
# 追加後
$ cat Cargo.toml
[package]
name = "sample-project"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ferris-says = "0.2"
# 依存関係のインストール
$ cargo build
次はソースを以下のようにいじります。
use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello fellow Rustaceans!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(message.as_bytes(), width, &mut writer).unwrap();
}
# 実行
$ cargo run
Compiling sample-project v0.1.0 (/home/vagrant/sample-project)
Finished dev [unoptimized + debuginfo] target(s) in 1.00s
Running `target/debug/sample-project`
__________________________
< Hello fellow Rustaceans! >
--------------------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \
おしゃれな表示方法に変わりました。
#用語の確認
##Rustup
Rustのインストーラおよびバージョン管理ツールです。
##cargo
Rustのビルドツールおよびパッケージマネージャです。
# プロジェクトのビルド
$ cargo build
# プロジェクトの実行
$ cargo run
# プロジェクトのテスト
$ cargo test
# プロジェクトのドキュメントのビルド
$ cargo doc
# ライブラリをcrates.ioに公開
$ cargo publish
#おわりに
とりあえずなんちゃってプログラマーでも触れることはできました。
次はREST API形式でものを作ってみたいと思います。
P.S.
Rustのキャラは可愛くて安心しましたw
(直近触れた言語がGoだったもので。。。)