LoginSignup
1

More than 1 year has passed since last update.

RailsエンジニアがRustでWebアプリを作るまで(1日目)

Last updated at Posted at 2022-09-26

概要と注意

  • 本記事は、完全未学習の状態からRustでAPIを書くまでの学習過程です。
  • 筆者が普段主務で使っているのがRuby(Rails)のため、Railsに例えた比喩が度々出ます。
  • Rustに関しての正確な説明はしていません。それどころか、間違った説明が度々出ます。あくまで学習の記録として読んでください。(訂正のコメントは大歓迎であります。)
  • 習得したら、全ての過程を1つの記事にまとめます

1日目 (学習時間: 1h)

Rustをinstallするよ!

兎にも角にもまずは環境を作らねばということで、Rustのinstall方法を検索
ありがたいことに公式の日本語ドキュメントがあったので、書いている通りにinstall

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 使っているshellでPATHを通す

ふむ、はいったようだ

> rustup --version
rustup 1.25.1 (bb60b1e89 2022-07-12)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.64.0 (a55dd71d5 2022-09-19)`

rubyだとrbenvのようなversion管理ツールが存在するが、Rustの場合はこのrustupというのが一種のバージョン管理ツールのようになっていて、rustup install stableのような形で安定版を持って来れるそうな。(Rust versionツールで調べるとrsvmっていうのがひっかかったが、何に使うのかはよくわかっていない)

次はrustcを入れる (rustcが何かはわかっていない)

> rustup install stable
info: syncing channel updates for 'stable-x86_64-apple-darwin'

  stable-x86_64-apple-darwin unchanged - rustc 1.64.0 (a55dd71d5 2022-09-19)

info: checking for self-updates
> rustc --version
rustc 1.64.0 (a55dd71d5 2022-09-19)

これも入ったっぽい 何かは知らんけど

Hello World

Rustの環境構築ができたところで、とりあえずHello Worldしていく。
Rustのプロジェクトはcargoというものを使って立ち上げるそうな とりあえずやってみよう。

> cargo new hello-world
Created binary (application) `hello-world` package

プロジェクトディレクトリが作成された。中身を見てみよう

> cd hello-world/
> ls
Cargo.toml  src/
> cd src
> ls

Cargo.tomlは中身見る感じ、package.json的なやつらしい。依存しているライブラリもここに記載していくようだ。
main.rsの中身は、printだけの単純なやつだった。functionはfnと略すんだな

fn main() {
    println!("Hello, world!");
}

中身を確認したところでいざ実行

> cargo run
   Compiling hello-world v0.1.0 (/Users/nakaofumiaki/hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.68s
     Running `/Users/*/hello-world/target/debug/hello-world`
Hello, world!

無事hello worldが出力された :tada:

RustのWeb Application Framework "axum"

Rustの環境構築ができたところで、いよいよWeb Application Frameworkを調べていく。
検索するとaxumというのがひっかかった。というか、書かれている記事がほぼこれなので、現時点ではこれ一択なのかもしれない

同じようにプロジェクト作成。リポジトリに入っていたサンプルプログラムをまるっとコピーして動かしてみる

localhost:3000にアクセスするとhello worldが出力された。いいねいいね

動いたので、次は軽く変更加えてみる
見様見真似で、/にアクセスした際の返り値をjsonに変更しようとしてみる

async fn root() -> &'static str {
    let rootMessage = RootMessage {
        message: 'hello world!',
    };

    (StatusCode::OK, Json(rootMessage))
}

#[derive(Deserialize)]
struct RootMessage {
  message: String,
}

実行 が、以下のようにエラー発生

error[E0762]: unterminated character literal
  --> src/main.rs:35:31
   |
35 |         message: 'hello world!',
   |                               ^^

For more information about this error, try `rustc --explain E0762`

どうも文法エラーらしい。試しに書かれていたrustc --explain E0762を試してみる

A character literal wasn't ended with a quote.

Erroneous code example:

static C: char = '; // error!

To fix this error, add the missing quote:

static C: char = '●'; // ok!

なんだこれすごい  文法のエラーがすごい詳細に出ている
どうやらシングルクォートが閉じられていないようだが...staticの前の&'が怪しいような気もする
ここら辺でRust自体のドキュメントを軽く読んでみたい気持ちになる

1日目まとめ

  • Rustの環境構築DONE
  • Hello Worldできた
  • WAF発見 動かすところまではできた :tada:

2日目に続く...!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1